サービスを開始します。
オーバーロードの一覧
引数を渡さずに、サービスを開始します。
[Visual Basic] Overloads Public Sub Start()
[C#] public void Start();
[C++] public: void Start();
[JScript] public function Start();
指定した引数を渡して、サービスを開始します。
[Visual Basic] Overloads Public Sub Start(String())
[JScript] public function Start(String[]);
使用例
[Visual Basic, C#, C++] ServiceController クラスを使用して Alerter サービスが停止しているかどうかを確認する例を次に示します。サービスが停止している場合、このコードはサービスを開始し、サービス ステータスが Running に設定されるまで待機します。
[Visual Basic, C#, C++] メモ ここでは、Start のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
' 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
[C#]
// 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.ToString());
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.ToString());
}
catch (InvalidOperationException)
{
Console.WriteLine("Could not start the Alerter service.");
}
}
[C++]
// Check whether the Alerter service is started.
ServiceController *sc = new ServiceController();
if (sc)
{
sc->ServiceName = "Alerter";
Console::WriteLine("The Alerter service status is currently set to {0}",
__box(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}.",
__box(sc->Status));
}
catch (InvalidOperationException *e)
{
Console::WriteLine("Could not start the Alerter service.");
}
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
参照
ServiceController クラス | ServiceController メンバ | System.ServiceProcess 名前空間