ServiceController 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 Windows 服務,允許你連接正在運行或停止的服務、操作服務或取得相關資訊。
public ref class ServiceController : System::ComponentModel::Component
public class ServiceController : System.ComponentModel.Component
[System.ServiceProcess.ServiceProcessDescription("ServiceControllerDesc")]
public class ServiceController : System.ComponentModel.Component
type ServiceController = class
inherit Component
[<System.ServiceProcess.ServiceProcessDescription("ServiceControllerDesc")>]
type ServiceController = class
inherit Component
Public Class ServiceController
Inherits Component
- 繼承
- 屬性
範例
以下範例展示如何使用類別 ServiceController 來控制 SimpleService 服務範例。
using System;
using System.ServiceProcess;
using System.Diagnostics;
using System.Threading;
namespace ServiceControllerSample
{
class Program
{
public enum SimpleServiceCustomCommands
{ StopWorker = 128, RestartWorker, CheckWorker };
static void Main(string[] args)
{
ServiceController[] scServices;
scServices = ServiceController.GetServices();
foreach (ServiceController scTemp in scServices)
{
if (scTemp.ServiceName == "Simple Service")
{
// Display properties for the Simple Service sample
// from the ServiceBase example.
ServiceController sc = new ServiceController("Simple Service");
Console.WriteLine("Status = " + sc.Status);
Console.WriteLine("Can Pause and Continue = " + sc.CanPauseAndContinue);
Console.WriteLine("Can ShutDown = " + sc.CanShutdown);
Console.WriteLine("Can Stop = " + sc.CanStop);
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
while (sc.Status == ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
}
// Issue custom commands to the service
// enum SimpleServiceCustomCommands
// { StopWorker = 128, RestartWorker, CheckWorker };
sc.ExecuteCommand((int)SimpleServiceCustomCommands.StopWorker);
sc.ExecuteCommand((int)SimpleServiceCustomCommands.RestartWorker);
sc.Pause();
while (sc.Status != ServiceControllerStatus.Paused)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
sc.Continue();
while (sc.Status == ServiceControllerStatus.Paused)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
sc.Stop();
while (sc.Status != ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
String[] argArray = new string[] { "ServiceController arg1", "ServiceController arg2" };
sc.Start(argArray);
while (sc.Status == ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
Console.WriteLine("Status = " + sc.Status);
// Display the event log entries for the custom commands
// and the start arguments.
EventLog el = new EventLog("Application");
EventLogEntryCollection elec = el.Entries;
foreach (EventLogEntry ele in elec)
{
if (ele.Source.IndexOf("SimpleService.OnCustomCommand") >= 0 |
ele.Source.IndexOf("SimpleService.Arguments") >= 0)
Console.WriteLine(ele.Message);
}
}
}
}
}
}
// 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
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
End Class
' 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
備註
你可以用這個 ServiceController 類別連接並控制現有服務的行為。 當你建立該類別的實例 ServiceController 時,你會設定它的屬性,使其與特定的 Windows 服務互動。 接著你可以用這個類別來啟動、停止或以其他方式操作服務。
你很可能會以行政身份使用這個 ServiceController 元件。 例如,你可以建立一個 Windows 或網頁應用程式,透過實 ServiceController 例向服務發送自訂指令。 這很有用,因為 Microsoft 管理控制台的服務控制管理器(SCM)不支援自訂指令。
在建立一個 ServiceController實例 後,必須設定兩個屬性來識別它所互動的服務:電腦名稱和你想控制的服務名稱。
Note
預設情況下, MachineName 是設定為本地電腦,除非你想讓實例指向另一台電腦,否則不需要更改。
通常,服務作者會撰寫程式碼,來自訂與特定指令相關的動作。 例如,一個服務可以包含回應 ServiceBase.OnPause 指令的程式碼。 在這種情況下,該任務的自訂處理 Pause 會在系統暫停服務之前執行。
服務能處理的指令集合取決於其屬性;例如,你可以將某項服務的屬性設定 CanStop 為 false。 此設定會使 Stop 該指令在該服務中無法使用;它阻止你透過停用必要的按鈕來停止服務。 如果你嘗試從程式碼停止服務,系統會跳出錯誤並顯示「Failed to stop servicename.」的錯誤訊息。
建構函式
| 名稱 | Description |
|---|---|
| ServiceController() |
初始化一個與特定服務無關聯的類別新實例 ServiceController 。 |
| ServiceController(String, String) |
初始化與指定電腦上現有服務相關的類別新實例 ServiceController 。 |
| ServiceController(String) |
初始化一個與本地電腦上現有服務相關的類別新實例 ServiceController 。 |
屬性
| 名稱 | Description |
|---|---|
| CanPauseAndContinue |
會獲得一個值,表示服務是否可以暫停並恢復。 |
| CanRaiseEvents |
會得到一個值,表示該元件是否能引發事件。 (繼承來源 Component) |
| CanShutdown |
會獲得一個值,指示系統關閉時是否應該通知服務。 |
| CanStop |
會獲得一個值,表示服務在啟動後是否可以停止。 |
| Container |
得到 IContainer 包含 Component的 。 (繼承來源 Component) |
| DependentServices |
取得依賴於該 ServiceController 實例所關聯服務的服務集合。 |
| DesignMode |
會得到一個值,表示目前 Component 是否處於設計模式。 (繼承來源 Component) |
| DisplayName |
為服務取一個親切的名字。 |
| Events |
會取得與此 Component連結的事件處理程序清單。 (繼承來源 Component) |
| MachineName |
取得或設定此服務所在電腦的名稱。 |
| ServiceHandle |
他負責服務。 |
| ServiceName |
取得或設定該實例所參考服務的名稱。 |
| ServicesDependedOn |
這項服務所依賴的服務集合。 |
| ServiceType |
取得該物件所參考的服務類型。 |
| Site | (繼承來源 Component) |
| StartType |
會得到一個值,表示該物件所代表 ServiceController 的服務是如何開始的。 |
| Status |
取得此實例所參考服務的狀態。 |
方法
事件
| 名稱 | Description |
|---|---|
| Disposed |
當元件被呼叫方法 Dispose() 時會發生。 (繼承來源 Component) |