dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Write Your Own Windows Services : Page 2

If you're using Windows, you're no stranger to Windows services, but do you know how to write one of your own? In this article, you'll learn not only how to build your own Windows service, but how to build a client with which to control it.


advertisement
Programmatically Control Windows Services
Before you see how you can create your own Windows Services, let's take a look at how you can programmatically control the Windows services currently running on your system.

First, to list all the Windows services currently installed on your system, use the GetServices() method from the ServiceController class, available from the System.ServiceProcess.dll:


        '---retrieve all the services installed on the system---
        Dim services() As System.ServiceProcess.ServiceController
        services = ServiceProcess.ServiceController.GetServices()
        For i As Integer = 0 To services.Length - 1
            Console.WriteLine(services(i).DisplayName)
        Next
The DisplayName property returns the name of the Windows service. The above code snippet prints out the list of Windows services I have on my computer (partial list shown below):

Alerter
Application Layer Gateway Service
Application Management
ASP.NET State Service
ATK Keyboard Service
Windows Audio
Background Intelligent Transfer Service
BrSplService
Computer Browser
Bluetooth Support Service
Indexing Service
...
To obtain the status of each Windows service, use the Status property from the ServiceController class:

        '---retrieve all the services installed on the system and their 
        ' status---
        Dim services() As System.ServiceProcess.ServiceController
        services = ServiceProcess.ServiceController.GetServices(".") 
        For i As Integer = 0 To services.Length - 1
            Console.WriteLine(services(i).DisplayName & " - " & _
            services(i).Status.ToString)
        Next
The following partial list shows the states of the various Windows services running on my computer:

Alerter - Stopped
Application Layer Gateway Service - Running
Application Management - Stopped
ASP.NET State Service - Stopped
ATK Keyboard Service - Running
Windows Audio - Running
Background Intelligent Transfer Service - Stopped
BrSplService - Running
Computer Browser - Running
Bluetooth Support Service - Running
Indexing Service – Stopped
...
To change the status of a Windows service, you can use the following methods from the ServiceController class:
  • Pause(): This pauses a running Windows service.
  • Continue(): This continues a Windows service that is currently paused.
  • Start(): This starts a Windows service.
  • Stop(): This stops a currently running (or paused) Windows service.
The following code snippet shows how you can control the Themes service running on your local computer (as specified by the “.”; you can also use the local computer name):

        '---control the Themes service running on the local computer---
        Dim controller As New _
           System.ServiceProcess.ServiceController("Themes", ".")

        '---if the service is running and it can be paused and 
        ' continued---
        If controller.Status = _
           ServiceProcess.ServiceControllerStatus.Running And _
           controller.CanPauseAndContinue Then
            '---pause the service---
            controller.Pause()
            '---continue the service---
            controller.Continue()

        ElseIf controller.CanStop Then
            '---stop the service---
            controller.Stop()
        End If
Here, you first check to see if the Themes service is currently running and whether it can be paused (via the CanPauseAndContinue property from the ServiceController class). If it is currently running and can be paused, you then pause the service and then immediately resume the service. If not, you simply stop the service.

It is important that you check the status of a Windows service before you change its state. A Windows service can be in one of the following states:

  • ContinuePending
  • Paused
  • PausePending
  • Running
  • StartPending
  • Stopped
  • StopPending
Consider the following example:

        '---start the service---
        controller.Start()

        controller.Refresh()
        '---will print StartPending---
        Console.WriteLine(controller.Status.ToString)        
        System.Threading.Thread.Sleep(1000)

        controller.Refresh()
        '---will print Running---
        Console.WriteLine(controller.Status.ToString) 
        System.Threading.Thread.Sleep(1000)
Here, you first start a Windows service and then print out its status two times, each within a one second interval. You will realize that immediately after starting the service, the status printed will be "StartPending", and one second later, the status becomes Running. It's important that you call the Refresh() method from the ServiceController class so that you can always obtain the latest status of the Windows service.

The following example demonstrates the importance of knowing the status of a Windows service:


        '---start the service---
        controller.Start()
        '---stop the service---
        controller.Stop()
The above will result in a runtime error because the Windows service is still starting up after executing the Start() method and any attempt to stop the service will trigger an InvalidOperationException exception.

To prevent this exception from happening, the above code snippet should be written this way:


        '---start the service---
        controller.Start()

        '---get the updated status---
        controller.Refresh()

        If controller.Status = _
            ServiceProcess.ServiceControllerStatus.Running Or _
           controller.Status = _
            ServiceProcess.ServiceControllerStatus.Paused Then
            '---stop the service---
            controller.Stop()
        End If


Thanks for your registration, follow us on our social networks to keep up-to-date