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
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 11 of 11.
-
Installing on a client machine
2008-04-10 15:40:20 Aaron Edwards [Reply | View]
Excellent Article!!!!
If you are installing on a client machine that doesn't have Visual Studio installed, you have to first navigate to your .NET framework folder, usually C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322, and type in
installUtil <PathToExecutable>
However: if there are any spaces in the path, it won't work. Keep this in mind.
-
Managing Windows Services
2007-07-12 12:48:41 ryanturner.com [Reply | View]
You Guys might be interested in this free tool for managing windows services. I particularly use it to group services I have created versus the windows ones. It kicks the chocolate out of the MMC.
www.mindswarm.com
-
Problem in starting window service
2006-10-17 22:02:39 Renukadevi [Reply | View]
Hi,
I have created a simple windows service in .NET and installed it without any trouble. Then i tried to start the service through Computer Management screen, its throwing message box saying "This service has been started and stopped. Some service stop automatically if they have no work to do."
But i have created the service with timer to trigger every 30 sec to write an entry to a database table.
Looks like i cannot start the service. Also i tried to debug it but i could not find my service.exe in the list to attach since it is not running. {instead i found explorer.exe which is new and from the same path my service.exe exists. But physically explorer.exe is not existing in that folder)
Could you please tell me what may be the cause? and how to fix this?
Thanks in advance
Renu
-
Can't put tcpClient to work
2005-12-19 12:32:10 CRodrigues [Reply | View]
Hi,
I develop a windows service to work with a tcpClient object.
The code works fine if I use a Windows App, but it does not work if the same code is placed in a Windows Service.
The message that is thrown is:
"No connection could be made because the target machine actively refused it"
Can someone give me a help?
Carlos
Regards
-
Error 1053: The service did not respond to the start or control request in a timely fashion.
2004-12-09 09:49:51 sekaran [Reply | View]
Hi,
Developed a windows services. Successfully installed and started manually fine. When i tried to stop it gives the following error.
Error 1053: The service did not respond to the start or control request in a timely fashion.
Microsoft website advised to install service pack 1 to solve the problem. But it didn't.
I thought that the account informations may cause the error. So i just copied the source for your reference.
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = "somthing";
this.serviceProcessInstaller1.Username = "RAJNEW\\raj";
RAJNEW is my computer name
raj is username
Is there anything am i doing wrong?
Any help would be appreciated.
Thanks,
Raj.
-
Sending parameters to windows services
2004-01-03 00:42:38 anonymous2 [Reply | View]
I need to send parameters to a windows service,
how can i do that. The service invokes automatically when windows launches
-
Uses of a Windows Service
2003-12-24 19:15:38 anonymous2 [Reply | View]
That article was great, explained alot. I am creating a basic online game and for my server would a windows service be recommended? Or just a windows application that ran in the task bar?





