共用方式為


ServiceController 類別

定義

代表 Windows 服務,並可讓您連線到執行或停止的服務、操作它或取得其相關信息。

public ref class ServiceController : System::ComponentModel::Component
public ref class ServiceController : IDisposable
public class ServiceController : System.ComponentModel.Component
public class ServiceController : IDisposable
[System.ServiceProcess.ServiceProcessDescription("ServiceControllerDesc")]
public class ServiceController : System.ComponentModel.Component
type ServiceController = class
    inherit Component
type ServiceController = class
    interface IDisposable
[<System.ServiceProcess.ServiceProcessDescription("ServiceControllerDesc")>]
type ServiceController = class
    inherit Component
Public Class ServiceController
Inherits Component
Public Class ServiceController
Implements IDisposable
繼承
ServiceController
繼承
ServiceController
屬性
實作

範例

以下範例展示如何使用類別 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實例 後,必須設定兩個屬性來識別它所互動的服務:電腦名稱和你想控制的服務名稱。

備註

預設情況下, MachineName 是設定為本地電腦,除非你想讓實例指向另一台電腦,否則不需要更改。

通常,服務作者會撰寫程式碼,來自訂與特定指令相關的動作。 例如,一個服務可以包含回應 ServiceBase.OnPause 指令的程式碼。 在這種情況下,該任務的自訂處理 Pause 會在系統暫停服務之前執行。

服務能處理的指令集合取決於其屬性;例如,你可以將某項服務的屬性設定 CanStopfalse。 此設定會使 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

取得或設定 ISiteComponent

(繼承來源 Component)
StartType

會得到一個值,表示該物件所代表 ServiceController 的服務是如何開始的。

Status

取得此實例所參考服務的狀態。

方法

名稱 Description
Close()

會將此 ServiceController 實例與服務斷開連結,並釋放該實例分配的所有資源。

Continue()

服務暫停後仍會繼續。

CreateObjRef(Type)

建立物件,其中包含產生用來與遠端物件通訊之 Proxy 所需的所有相關信息。

(繼承來源 MarshalByRefObject)
Dispose()

執行與釋放、釋放或重置未管理資源相關的應用程式定義任務。

Dispose()

釋放所有由 Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 未管理的資源, ServiceController 並可選擇性地釋放受管理資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
ExecuteCommand(Int32)

在服務上執行自訂指令。

GetDevices()

在本地電腦上取得裝置驅動程式服務。

GetDevices(String)

取得指定電腦上的裝置驅動程式服務。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個實例存留期原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

回傳一個由 或Component其 所提供的Container服務的物件。

(繼承來源 Component)
GetServices()

擷取本地電腦上的所有服務,唯獨裝置驅動程式服務除外。

GetServices(String)

擷取指定電腦上的所有服務,唯獨裝置驅動程式服務除外。

GetType()

取得目前實例的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個實例的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立一個 MarshalByRefObject 目前物件的淺層複製品。

(繼承來源 MarshalByRefObject)
Pause()

暫停服務運作。

Refresh()

透過將屬性重置為目前的值來刷新屬性值。

Start()

啟動服務,沒有提出任何爭議。

Start(String[])

啟動服務,傳遞指定的參數。

Stop()

停止此服務及任何依賴此服務的服務。

Stop(Boolean)

停止服務,並可選擇性地停止依賴此服務的任何服務。

ToString()

回傳 String 包含 的名稱 Component(若有的話)。 不應該覆寫這個方法。

(繼承來源 Component)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
WaitForStatus(ServiceControllerStatus, TimeSpan)

等待服務達到指定狀態或指定的逾時結束。

WaitForStatus(ServiceControllerStatus)

無限等待服務達到指定狀態。

事件

名稱 Description
Disposed

當元件被呼叫方法 Dispose() 時會發生。

(繼承來源 Component)

適用於

另請參閱