ServiceController.Stop 方法
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
Stop() |
停止该服务以及任何依赖于该服务的服务。 |
Stop(Boolean) |
停止服务以及依赖于此服务的任何服务(可选)。 |
- 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 包含此服务所依赖的服务。
另请参阅
适用于
.NET 9 (package-provided) 和其他版本
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided) |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
- 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
以仅停止服务。
注解
如果任何其他服务依赖于此服务,则需要在调用此方法之前手动传递true
stopDependentServices
或停止它们。
适用于
.NET 9 (package-provided) 和 .NET 8 (package-provided)
产品 | 版本 |
---|---|
.NET | 8 (package-provided), 9 (package-provided) |