Microsoft Windows services are long-running executables that perform specific functions in their own Windows sessions and do not require user intervention. They can be configured to start automatically whenever the system starts or can be started manually. These features make services ideal for use on a system user need long-running functionality without intervention. Windows services can be associated to particular user on the system.
Windows Services are used in following scenarios:
• Wait for incoming requests and Monitor a queue, file system, watching a folder or time event call etc.
• If a program just needs to run periodically, like on every hour. It is normally easier to create a scheduled task using windows service.
Windows Service runs in the background, withoutuser interaction.
Here are steps to create a windows service:
• Open Visual Studio -> Click on File -> New Project.
• Select language -> Click on Windows -> Windows Service
• It will create a new project with service.vb file
• Click on save
• And Service.vb file will have following code
Public Class Service1 Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. End Sub End Class
• Add following code to service.vb file
Imports System.IO Public Class Service1 Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set things ' in motion so your service can do its work. EventLog.WriteEntry("Service started......") File.Copy("c:\test.txt", "d:\test.txt") File.Delete("c:\test.txt") EventLog.WriteEntry("File moved.") End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. EventLog.WriteEntry("Service stopped......") End Sub End Class
• Now we need to add installer. Go to design view of service.vb and right click then select add installer
• Now projectInstaller.vb file got added into project. Open design view and set properties for both ServiceProcessInstaller1 and ServiceInstaller1 as mentioned below.
• Now we need to add setup project for windows service. Go to file -> Add -> New Project
• Then go to Other Project types -> Setup and Deployment -> Setup Project . Give some name to project and click OK.
• Now we need to add Service.vb to the setup project. Right click on setup project and select Add -> Project Output
• Then select TestWindowsService from Project dropdown and select Primary Output then click OK.
• Now we need to add custom action to the setup project. Right click on setup project and select Custom actions. Then right click on Custom Actions and click on Add Custom Action. Then double click on Application folder and select Primary Output from TestWindowsService. Then click OK and Rebuild the project.
• Now go into service location and double click on setup file. Then click next, Select Everyone and Click next. Service will get installed then finally click Close.
• Finally go to services and right click on service then click start. Service will get started.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)