ServiceController.Stop 方法

定义

重载

Stop()

停止该服务以及任何依赖于该服务的服务。

Stop(Boolean)

停止服务以及依赖于此服务的任何服务(可选)。

Stop()

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
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)

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
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或停止它们。

适用于