WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Developing Windows Services
Pages: 1, 2, 3

Controlling Windows Services using the ServiceController Class

Once a Windows Service is installed, you can control its state using codes. In this section, I will illustrate controlling a Windows Service using the ServiceController class. The ServiceController class allows a Windows Service to be started, stopped, paused, or continued.

1. Create a Windows application and name it WinAppServiceController.
2. On the toolbox, click on the Components tab and double-click on ServiceController (see Figure 10).

Figure 10. Adding the ServiceController control
Figure 10. Adding the ServiceController control

3. Change the MachineName and ServiceName properties of ServiceController1 to the machine name and the name of the Windows Service to control, respectively (see Figure 11).

Figure 11. Changing the MachineName and ServiceName properties
Figure 11. Changing the MachineName and ServiceName properties

4. Add the following buttons to your Windows Form (see Figure 12):

Figure 12. Creating the Windows application
Figure 12. Creating the Windows application

5. Key in the codes shown in bold below:


Private Sub Button1_Click_1(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button1.Click
  ServiceController1.Start()   ' for the Start button
End Sub

Private Sub Button2_Click_1(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button2.Click
  ServiceController1.Pause()   ' for the Pause button
End Sub

Private Sub Button3_Click_1(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button3.Click
  ServiceController1.Stop()    ' for the Stop button
End Sub

Private Sub Button4_Click_1(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button4.Click
    '  for the Check Status button
  ServiceController1.Refresh()
  MsgBox(ServiceController1.Status.ToString)
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button9.Click
  ServiceController1.Continue()  ' for the continue button
End Sub

That's about it. Press F5 to test your application.

Note that some Windows Services do not have an onPause() method. Therefore, before a service is paused, it is advisable for you to check whether it can be paused, or a runtime error will occur. You can do so using the CanPauseAndContinue property.


If ServiceController1.CanPauseAndContinue Then
  ServiceController1.Pause()   ' for the Pause button
Else
  MsgBox("Service cannot be paused.")
End If

Besides using the ServiceController control from the toolbox, the ServiceController can also be invoked from code, as the following code shows:


Dim controller As New System.ServiceProcess.ServiceController("TimeService")

Retrieving the List of Windows Services Running on Your System

Besides controlling a single Windows Service, you can also retrieve the list of Windows Services running on your system. In this section, I will extend the Windows application built in the previous section to get a list of all of the Windows Services installed on your system and to be able to start, stop, or pause the services.

1. Using the application built earlier, extend the Windows form to incorporate the following Button, Label, and ListBox controls (see Figure 13).

Figure 13. Extending the Windows application
Figure 13. Extending the Windows application

2. Type in the following code:


Private Sub Button6_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button6.Click
  '---for the Start button---
  Dim controller As New _
     System.ServiceProcess.ServiceController(ListBox1.SelectedItem)
  controller.Start()
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button7.Click
  '---for the Pause button---
  Dim controller As New _
    System.ServiceProcess.ServiceController(ListBox1.SelectedItem)
  If controller.CanPauseAndContinue Then
    controller.Pause()
  Else
    MsgBox("Service cannot be paused")
  End If
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button8.Click
  '---for the Stop button---
  Dim controller As New _
    System.ServiceProcess.ServiceController(ListBox1.SelectedItem)
  controller.Stop()
End Sub

Private Sub Button5_Click_1(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button5.Click
  '---for the Get Service button---
  Dim controller As New System.ServiceProcess.ServiceController()
  Dim services() As System.ServiceProcess.ServiceController
  Dim i As Integer

  services = controller.GetServices()
  For i = 0 To services.Length - 1
    Me.ListBox1.Items.Add(services(i).DisplayName)
  Next
End Sub

Private Sub ListBox1_SelectedIndexChanged_1(ByVal sender As _
                     System.Object, ByVal e As System.EventArgs) _
                     Handles ListBox1.SelectedIndexChanged
  '---when the listbox is clicked---
  Dim controller As New _
    System.ServiceProcess.ServiceController(ListBox1.SelectedItem)
  Label1.Text = ListBox1.SelectedItem & " - " & _
    controller.Status.ToString
End Sub

To test the application, press F5. You should see something like Figure 14. Click on the Start, Pause, and Stop buttons to change the state of the services.

Figure 14. Getting the list of Windows Services running on the current system
Figure 14. Getting the list of Windows Services running on the current system

Summary

Writing Windows Services is now much easier using Visual Studio .NET. However, debugging Windows Services is still a pretty challenging task. For that, I suggest that you test out your code in a Windows application and ascertain that it works before porting it to a Windows Service application. Have fun!

Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.


Return to ONDotnet.com