Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Windows Services

DZone's Guide to

Windows Services

·
Free Resource

This article was written by the Imaginovation team.  They are a Raleigh web design and software development company who uses .NET, PHP, HTML5, JavaScript, and jQuery technologies.

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.

Topics:

Published at DZone with permission of Michael Georgiou. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}