How to Write a Windows Service in .Net 2.0 I will show you how to write a windows service using vb.

net step by step and create a step project for that service Open File New Project Visual Basic Windows Then Select Windows Service

Then The Screen will look like this

Select Service1.vb and View Code to Write your own Code

Public Class Service1 Dim t As Timer Protected Overrides Sub OnStart(ByVal args() As String) Try t = New Timer(2000) AddHandler t.Elapsed, AddressOf MyProcess With t .AutoReset = True .Enabled = True .Start() End With Catch obug As Exception Throw obug End Try End Sub

Protected Overrides Sub OnStop() Try t.Stop() t.Dispose() Catch obug As Exception Throw obug End Try End Sub Private Sub MyProcess(ByVal sender As Object, ByVal e As ElapsedEventArgs) //Write Your Own Code to Do Process End Sub End Class

1. Return to design view for Service1. 2. Click the background of the designer to select the service itself, rather than any of its contents.

3. In the Properties window, click the Add Installer link in the gray area beneath the list of properties. By default, a component class containing two installers is added to your project. The component is named ProjectInstaller, and the installers it contains are the installer for your service and the installer for the service's associated process. The Screen Looks like this

4. Access design view for ProjectInstaller, and click ServiceInstaller1. 5. In the Properties window, set the ServiceName property to your service name.

6. Set the StartType property to Automatic/Manual.

To avoid being asked about the system username and password you must change the Account for the serviceProcessInstaller to LocalSystem. This is done by opening the ProjectInstaller design and then selecting the serviceProcessInstaller, press F4 and then change the Account property to LocalSystem.

To build your service project
1. In Solution Explorer, right-click your project and select Properties from the shortcut menu. The project's Property Pages dialog box appears.

2. In the left pane, select the Application tab. 3. From the Startup object list, choose Service1. Click OK. 4. Press Ctrl+Shift+B to build the project.

Now that the project is built, it can be deployed. A setup project will install the compiled project files and run the installers needed to run the Windows service. To create a complete setup project, you will need to add the project output, Service1.exe, to the setup project and then add a custom action to have Service1.exe installed.

To create a setup project for your service
1. On the File menu, point to Add Project, and then choose New Project. 2. In the Project Types pane, select the Setup and Deployment Projects folder. 3. In the Templates pane, select Setup Project. Name the project Setup1.

A setup project is added to the solution. Next you will add the output from the Windows service project, WindowsService1.exe, to the setup. And the screen will look like this.

To add WindowsService1.exe to the setup project
1. In Solution Explorer, right-click Setup1, point to Add, then choose Project Output. The Add Project Output Group dialog box appears.

2. WindowsService1 is selected in the Project box. 3. From the list box, select Primary Output, and click OK.
A project item for the primary output of WindowsService1 is added to the setup project. Now add a custom action to install the WindowsService1.exe file.

To add a custom action to the setup project
1. In Solution Explorer, right-click the setup project, point to View, then choose Custom Actions. The Custom Actions editor appears.

2. In the Custom Actions editor, right-click the Custom Actions node and choose Add Custom Action.
The Select Item in Project dialog box appears.

3. Double-click the application folder in the list box to open it, select primary output from
WindowsService1 (Active), and click OK. The primary output is added to all four nodes of the
custom actions � Install, Commit, Rollback, and Uninstall.

4. Build the setup project.

Then Locate the Setup1.msi and Click to Install the Service

After installing the setup1.msi open the services window to start the windowservice1 if you have selected Manual startup

Sign up to vote on this title
UsefulNot useful