ServiceController.Start 方法

定義

啟動服務。

多載

Start()

啟動服務,不傳遞引數。

Start(String[])

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

Start()

來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs

啟動服務,不傳遞引數。

public:
 void Start();
public void Start ();
member this.Start : unit -> unit
Public Sub Start ()

例外狀況

存取系統 API 時發生的錯誤。

找不到服務。

範例

下列範例會 ServiceController 使用 類別來檢查 Alerter 服務是否已停止。 如果服務已停止,此範例會啟動服務,並等到服務狀態設定為 Running 為止。

// Check whether the Alerter service is started.
ServiceController^ sc = gcnew ServiceController;
if ( sc )
{
   sc->ServiceName =  "Alerter";
   Console::WriteLine(  "The Alerter service status is currently set to {0}", sc->Status );
   if ( sc->Status == (ServiceControllerStatus::Stopped) )
   {
      // Start the service if the current status is stopped.
      Console::WriteLine(  "Starting the Alerter service..." );
      try
      {
         // Start the service, and wait until its status is "Running".
         sc->Start();
         sc->WaitForStatus( ServiceControllerStatus::Running );
         
         // Display the current service status.
         Console::WriteLine(  "The Alerter service status is now set to {0}.", sc->Status );
      }
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine(  "Could not start the Alerter service." );
      }
   }
}

// Check whether the Alerter service is started.

ServiceController sc  = new ServiceController();
sc.ServiceName = "Alerter";
Console.WriteLine("The Alerter service status is currently set to {0}",
                   sc.Status);

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

   Console.WriteLine("Starting the Alerter service...");
   try
   {
      // Start the service, and wait until its status is "Running".
      sc.Start();
      sc.WaitForStatus(ServiceControllerStatus.Running);

      // Display the current service status.
      Console.WriteLine("The Alerter service status is now set to {0}.",
                         sc.Status);
   }
   catch (InvalidOperationException)
   {
      Console.WriteLine("Could not start the Alerter service.");
   }
}

' Check whether the Alerter service is started.

Dim sc As New ServiceController()
sc.ServiceName = "Alerter"
Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status)

If sc.Status = ServiceControllerStatus.Stopped Then
   ' Start the service if the current status is stopped.
   Console.WriteLine("Starting the Alerter service...")

   Try
      ' Start the service, and wait until its status is "Running".
      sc.Start()
      sc.WaitForStatus(ServiceControllerStatus.Running)
      
      ' Display the current service status.
      Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status)
   Catch 
      Console.WriteLine("Could not start the Alerter service.")
   End Try
End If

備註

在服務控制器狀態為 Running 之前,您無法呼叫 Stop 服務。

另請參閱

適用於

Start(String[])

來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs
來源:
ServiceController.cs

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

public:
 void Start(cli::array <System::String ^> ^ args);
public void Start (string[] args);
member this.Start : string[] -> unit
Public Sub Start (args As String())

參數

args
String[]

要在服務啟動時傳遞給它的引數陣列。

例外狀況

存取系統 API 時發生的錯誤。

無法啟動服務。

argsnull

-或-

陣列的成員為 null

備註

在服務控制器狀態為 Running 之前,您無法呼叫 Stop 服務。

另請參閱

適用於