共用方式為


ServiceControllerStatus 列舉

定義

指示服務的目前狀態。

public enum class ServiceControllerStatus
public enum ServiceControllerStatus
type ServiceControllerStatus = 
Public Enum ServiceControllerStatus
繼承
ServiceControllerStatus

欄位

ContinuePending 5

服務繼續為暫止。 這會對應到 Win32 SERVICE_CONTINUE_PENDING 常數 (此數定義為 0x00000005)。

Paused 7

暫停服務。 這會對應到 Win32 SERVICE_PAUSED 常數 (此數定義為 0x00000007)。

PausePending 6

服務暫停為暫止。 這會對應到 Win32 SERVICE_PAUSE_PENDING 常數 (此數定義為 0x00000006)。

Running 4

服務正在執行。 這會對應到 Win32 SERVICE_RUNNING 常數 (此數定義為 0x00000004)。

StartPending 2

服務正在啟動。 這會對應到 Win32 SERVICE_START_PENDING 常數 (此數定義為 0x00000002)。

Stopped 1

沒有執行服務。 這會對應到 Win32 SERVICE_STOPPED 常數 (此數定義為 0x00000001)。

StopPending 3

服務正在停止。 這會對應到 Win32 SERVICE_STOP_PENDING 常數 (此數定義為 0x00000003)。

範例

下列範例會 ServiceController 使用 類別來檢查 TelNet 服務的目前狀態。 如果服務已停止,此範例會啟動服務。 如果服務正在執行,此範例會停止服務。

// Toggle the Telnet service - 
// If it is started (running, paused, etc), stop the service.
// If it is stopped, start the service.
ServiceController^ sc = gcnew ServiceController(  "Telnet" );
if ( sc )
{
   Console::WriteLine(  "The Telnet service status is currently set to {0}", sc->Status );
   if ( (sc->Status == (ServiceControllerStatus::Stopped) ) || (sc->Status == (ServiceControllerStatus::StopPending) ) )
   {
      // Start the service if the current status is stopped.
      Console::WriteLine(  "Starting the Telnet service..." );
      sc->Start();
   }
   else
   {
      // Stop the service if its status is not set to "Stopped".
      Console::WriteLine(  "Stopping the Telnet service..." );
      sc->Stop();
   }

   // Refresh and display the current service status.
   sc->Refresh();
   Console::WriteLine(  "The Telnet service status is now set to {0}.", sc->Status );

// Toggle the Telnet service -
// If it is started (running, paused, etc), stop the service.
// If it is stopped, start the service.
ServiceController sc = new ServiceController("Telnet");
Console.WriteLine("The Telnet service status is currently set to {0}",
                  sc.Status);

if ((sc.Status == ServiceControllerStatus.Stopped) ||
    (sc.Status == ServiceControllerStatus.StopPending))
{
   // Start the service if the current status is stopped.

   Console.WriteLine("Starting the Telnet service...");
   sc.Start();
}
else
{
   // Stop the service if its status is not set to "Stopped".

   Console.WriteLine("Stopping the Telnet service...");
   sc.Stop();
}

// Refresh and display the current service status.
sc.Refresh();
Console.WriteLine("The Telnet service status is now set to {0}.",
                   sc.Status);

' Toggle the Telnet service - 
' If it is started (running, paused, etc), stop the service.
' If it is stopped, start the service.
Dim sc As New ServiceController("Telnet")
Console.WriteLine("The Telnet service status is currently set to {0}", sc.Status)

If sc.Status.Equals(ServiceControllerStatus.Stopped) Or sc.Status.Equals(ServiceControllerStatus.StopPending) Then
   ' Start the service if the current status is stopped.
   Console.WriteLine("Starting the Telnet service...")
   sc.Start()
Else
   ' Stop the service if its status is not set to "Stopped".
   Console.WriteLine("Stopping the Telnet service...")
   sc.Stop()
End If

' Refresh and display the current service status.
sc.Refresh()
Console.WriteLine("The Telnet service status is now set to {0}.", sc.Status)

備註

類別 ServiceControllerStatusServiceController 實例會使用此列舉,以指出現有服務正在執行、停止、暫停,或是 Start、Stop、Pause 或 Continue 命令是否擱置中。

適用於

另請參閱