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 |
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 |
4. Add the following buttons to your Windows Form (see Figure 12):
|
| 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 |
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 |
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
-
Nice and thorough!
2009-08-16 01:21:55 mrt_doulaty [View]
-
Installing on a client machine
2008-04-10 15:40:20 Aaron Edwards [View]
-
Managing Windows Services
2007-07-12 12:48:41 ryanturner.com [View]
-
Problem in starting window service
2006-10-17 22:02:39 Renukadevi [View]
-
Can't put tcpClient to work
2005-12-19 12:32:10 CRodrigues [View]
- Trackback from http://jasonhorner.com/blog/archive/2005/10/22/159.aspx
Developing Windows Services
2005-10-22 09:06:39 [View]
- Trackback from http://www.techquik.com/archives/2005/06/building_net_wi.html
Building .NET Windows Services
2005-06-22 06:54:26 [View]
-
Error 1053: The service did not respond to the start or control request in a timely fashion.
2004-12-09 09:49:51 sekaran [View]
- Trackback from http://www.doesntsuck.com/archives/000759.html
Building Windows Services
2004-04-07 07:28:07 [View]
- Trackback from http://dotnetjunkies.com/weblog/peter marshall/posts/5371.aspx
Windows Services
2004-01-08 02:22:28 [View]
- Trackback from http://dotnetjunkies.com/weblog/peter marshall/posts/5371.aspx
Windoes Services
2004-01-08 02:22:11 [View]
-
Sending parameters to windows services
2004-01-03 00:42:38 anonymous2 [View]
-
Sending parameters to windows services
2004-01-14 13:50:53 anonymous2 [View]
-
Uses of a Windows Service
2003-12-24 19:15:38 anonymous2 [View]
-
Deployment
2003-10-14 15:42:34 anonymous2 [View]
-
it works, much like http://www.codon4.com
2003-08-19 18:56:09 anonymous2 [View]

