ServiceController Class
Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.
Assembly: System.ServiceProcess (in System.ServiceProcess.dll)
You can use the ServiceController class to connect to and control the behavior of existing services. When you create an instance of the ServiceController class, you set its properties so it interacts with a specific Windows service. You can then use the class to start, stop, and otherwise manipulate the service.
You will most likely use the ServiceController component in an administrative capacity. For example, you could create a Windows or Web application that sends custom commands to a service through the ServiceController instance. This would be useful, because the Service Control Manager (SCM) Microsoft Management Console snap-in does not support custom commands.
After you create an instance of ServiceController, you must set two properties on it to identify the service with which it interacts: the computer name and the name of the service you want to control.
Note: |
|---|
By default, MachineName is set to the local computer, so you do not need to change it unless you want to set the instance to point to another computer. |
Generally, the service author writes code that customizes the action associated with a specific command. For example, a service can contain code to respond to an ServiceBase.OnPause command. In that case, the custom processing for the Pause task runs before the system pauses the service.
The set of commands a service can process depends on its properties; for example, you can set the CanStop property for a service to false. This setting renders the Stop command unavailable on that particular service; it prevents you from stopping the service from the SCM by disabling the necessary button. If you try to stop the service from your code, the system raises an error and displays the error message "Failed to stop servicename."
The following example demonstrates the use of the ServiceController class to control the SimpleService service example. See the ServiceBase class for the example code for the SimpleService service.
Imports System Imports System.ServiceProcess Imports System.Diagnostics Imports System.Threading Class Program Public Enum SimpleServiceCustomCommands StopWorker = 128 RestartWorker CheckWorker End Enum 'SimpleServiceCustomCommands Shared Sub Main(ByVal args() As String) Dim scServices() As ServiceController scServices = ServiceController.GetServices() Dim scTemp As ServiceController For Each scTemp In scServices If scTemp.ServiceName = "Simple Service" Then ' Display properties for the Simple Service sample ' from the ServiceBase example Dim sc As New ServiceController("Simple Service") Console.WriteLine("Status = " + sc.Status.ToString()) Console.WriteLine("Can Pause and Continue = " + _ sc.CanPauseAndContinue.ToString()) Console.WriteLine("Can ShutDown = " + sc.CanShutdown.ToString()) Console.WriteLine("Can Stop = " + sc.CanStop.ToString()) If sc.Status = ServiceControllerStatus.Stopped Then sc.Start() While sc.Status = ServiceControllerStatus.Stopped Thread.Sleep(1000) sc.Refresh() End While End If ' Issue custom commands to the service ' enum SimpleServiceCustomCommands ' { StopWorker = 128, RestartWorker, CheckWorker }; sc.ExecuteCommand(Fix(SimpleServiceCustomCommands.StopWorker)) sc.ExecuteCommand(Fix(SimpleServiceCustomCommands.RestartWorker)) sc.Pause() While sc.Status <> ServiceControllerStatus.Paused Thread.Sleep(1000) sc.Refresh() End While Console.WriteLine("Status = " + sc.Status.ToString()) sc.Continue() While sc.Status = ServiceControllerStatus.Paused Thread.Sleep(1000) sc.Refresh() End While Console.WriteLine("Status = " + sc.Status.ToString()) sc.Stop() While sc.Status <> ServiceControllerStatus.Stopped Thread.Sleep(1000) sc.Refresh() End While Console.WriteLine("Status = " + sc.Status.ToString()) Dim argArray() As String = {"ServiceController arg1", "ServiceController arg2"} sc.Start(argArray) While sc.Status = ServiceControllerStatus.Stopped Thread.Sleep(1000) sc.Refresh() End While Console.WriteLine("Status = " + sc.Status.ToString()) ' Display the event log entries for the custom commands ' and the start arguments. Dim el As New EventLog("Application") Dim elec As EventLogEntryCollection = el.Entries Dim ele As EventLogEntry For Each ele In elec If ele.Source.IndexOf("SimpleService.OnCustomCommand") >= 0 Or ele.Source.IndexOf("SimpleService.Arguments") >= 0 Then Console.WriteLine(ele.Message) End If Next ele End If Next scTemp End Sub 'Main End Class 'Program ' This sample displays the following output if the Simple Service ' sample is running: 'Status = Running 'Can Pause and Continue = True 'Can ShutDown = True 'Can Stop = True 'Status = Paused 'Status = Running 'Status = Stopped 'Status = Running '4:14:49 PM - Custom command received: 128 '4:14:49 PM - Custom command received: 129 'ServiceController arg1 'ServiceController arg2
System.MarshalByRefObject
System.ComponentModel.Component
System.ServiceProcess.ServiceController
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: