ServiceController.Stop 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
Stop() |
停止這項服務和依賴這項服務的任何服務。 |
Stop(Boolean) |
停止服務,並選擇性地停止任何相依于此服務的服務。 |
Stop()
停止這項服務和依賴這項服務的任何服務。
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)
停止服務,並選擇性地停止任何相依于此服務的服務。
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
僅停止服務。
備註
如果任何其他服務相依于此服務,您必須在呼叫此方法之前手動傳遞 true
stopDependentServices
或停止它們。