Managing Windows Services with Visual Studio 2005 - Starting, Pausing and Stopping a Service
(Page 5 of 5 )
Service Status
These actions can be managed by using the methods of the Service Controller class. Some services have only starting and stopping while others may have the option of being paused. You may verify if the service has stopped or started by looking at its status property. This has a number of values given by their "enums" as shown in the next two pictures.


Stopping and Starting
The code shown in the next paragraph shows how you may stop and start the service whose name is OracleXEClrAgent programmatically.
Private Sub Button3_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button3.Click
Dim mysvcCntrl As New ServiceProcess.ServiceController
mysvcCntrl.ServiceName = "OracleXEClrAgent"
MessageBox.Show(mysvcCntrl.Status)
Try
If mysvcCntrl.Status = 4 Then
mysvcCntrl.Stop()
'does not refresh in the services panel
mysvcCntrl.Refresh()
MessageBox.Show(mysvcCntrl.Status)
End If
Catch ex As Exception
MessageBox.Show(ex.InnerException.ToString)
End Try
End Sub
In the above code the program will stop the service if it is running. If it is already stopped it may throw up an exception. When a value of 1 is used instead of 4 for status in the above code, you will get the following message about the exception.

The Service Controller's other properties can also be used by using the same code. You may look up the status codes, or you can alternatively use the syntax shown in the next paragraph.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim mysvcCntrl As New ServiceProcess.ServiceController
mysvcCntrl.ServiceName = "MSSQL$MYSORIAN"
With mysvcCntrl
MessageBox.Show(.DisplayName)
MessageBox.Show(.MachineName)
MessageBox.Show(.ServiceHandle.ToString)
End With
MessageBox.Show(mysvcCntrl.status)
If (mysvcCntrl.Status = ServiceControllerStatus.Stopped) _
Then
MessageBox.Show("Starting the SQL Server")
Try
mysvcCntrl.Start()
mysvcCntrl. _
WaitForStatus(ServiceControllerStatus.Running)
MessageBox.Show(mysvcCntrl.Status)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
After stopping or starting the service, the Service Control Manager's screen in the Control Panel does not get refreshed. To see if the change has taken place you need to refresh the view. Also to get the correct service name you should look up the property of the service in the services window, or use the code shown earlier which lists the name of the services.
Also make sure that the form on which all these various buttons are placed have the following "imports" statements.
Imports System.ServiceProcess.ServiceController
Imports System.ServiceProcess
Summary
This tutorial described how to manage the Windows service using the Control Panel in the Windows machine as well as programmatically using the appropriate namespace class in the Visual Studio 2005 IDE.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |