次の方法で共有


ServiceController.Start メソッド ()

引数を渡さずに、サービスを開始します。

Overloads Public Sub Start()
[C#]
public void Start();
[C++]
public: void Start();
[JScript]
public function Start();

例外

例外の種類 条件
Win32Exception システム API へのアクセス中にエラーが発生しました。
InvalidOperationException サービスを開始できません。

解説

サービス コントローラのステータスが Running になるまで、サービスの Stop を呼び出すことはできません。

使用例

[Visual Basic, C#, C++] ServiceController クラスを使用して Alerter サービスが停止しているかどうかを確認する例を次に示します。サービスが停止している場合、このコードはサービスを開始し、サービス ステータスが Running に設定されるまで待機します。

 

         ' 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

ServiceController クラス | ServiceController メンバ | System.ServiceProcess 名前空間 | ServiceController.Start オーバーロードの一覧 | Stop | Status