共用方式為


ServiceController.Stop 方法

定義

多載

名稱 Description
Stop()

停止此服務及任何依賴此服務的服務。

Stop(Boolean)

停止服務,並可選擇性地停止依賴此服務的任何服務。

Stop()

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

停止此服務及任何依賴此服務的服務。

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

例外狀況

存取系統 API 時發生錯誤。

但未找到該服務。

範例

以下範例使用 該 ServiceController 類別來檢查 Telnet 服務的當前狀態。 如果服務被停止,範例會啟動服務。 如果服務正在執行,範例會停止服務。

// Toggle the Telnet service - 
// If it is started (running, paused, etc), stop the service.
// If it is stopped, start the service.
ServiceController^ sc = gcnew ServiceController(  "Telnet" );
if ( sc )
{
   Console::WriteLine(  "The Telnet service status is currently set to {0}", 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}.", sc->Status );

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

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}.",
                   sc.Status);

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

備註

若有任何服務依賴此服務運作,該服務將在服務停止前被停止。 該 DependentServices 屬性包含依賴於此項的服務集合。

如果你停止了該服務所依賴的服務,請在父服務的方法中Stop呼叫Stop該服務的方法。 該 ServicesDependedOn 物業包含了這項服務所依賴的服務。

另請參閱

適用於

Stop(Boolean)

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

停止服務,並可選擇性地停止依賴此服務的任何服務。

public:
 void Stop(bool stopDependentServices);
public void Stop(bool stopDependentServices);
member this.Stop : bool -> unit
Public Sub Stop (stopDependentServices As Boolean)

參數

stopDependentServices
Boolean

true 停止所有運行中的相依服務與服務一同運作; false 只停止服務。

備註

如果其他服務依賴這個方法,你需要先手動通過truestopDependentServices或停止,然後再呼叫這個方法。

適用於