ServiceController.Start 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
啟動服務。
多載
Start() |
啟動服務,不傳遞引數。 |
Start(String[]) |
啟動服務,傳遞指定的引數。 |
Start()
啟動服務,不傳遞引數。
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 服務。