次の方法で共有


ServiceController.Stop メソッド

このサービスと、このサービスに依存しているすべてのサービスを停止します。

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

例外

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

解説

操作において、このサービスに依存しているサービスがある場合、このサービスが停止される前にそれらのサービスが停止されます。このサービスに依存している一連のサービスを格納している DependentServices プロパティ。

このサービスが依存しているサービスを停止する場合は、親サービスの Stop メソッド内で、このサービスに対して Stop メソッドを呼び出します。 ServicesDependedOn プロパティは、このサービスが依存しているサービスを格納しています。

使用例

[Visual Basic, C#, C++] ServiceController クラスを使用して、TelNet サービスの現在のステータスを確認する例を次に示します。サービスが停止している場合、このコードはサービスを開始します。サービスが実行中である場合は、そのサービスを停止します。

 

         ' 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)


[C#] 

         // 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.ToString());

         if  ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
              (sc.Status.Equals(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.ToString());
         

[C++] 

         // 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");

         if (sc)
         {
            Console::WriteLine("The Telnet service status is currently set to {0}", 
                              __box(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}.", 
                               __box(sc->Status));
         

[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 名前空間 | CanStop | Start | Status