ServiceController.Stop 方法

定義

停止這項服務和依賴這項服務的任何服務。Stops this service and any services that are dependent on this service.

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

例外狀況

存取系統 API 時發生的錯誤。An error occurred when accessing a system API.

找不到服務。The service was not found.

範例

下列範例會使用ServiceController類別來檢查 Telnet 服務的目前狀態。The following example uses the ServiceController class to check the current status of the Telnet service. 如果服務已停止,此範例會啟動服務。If the service is stopped, the example starts the service. 如果服務正在執行,此範例就會停止服務。If the service is running, the example stops the service.

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


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

備註

如果有任何服務相依于此服務以進行作業,則會在停止此服務之前將其停止。If any services depend on this service for their operation, they will be stopped before this service is stopped. DependentServices屬性包含相依于此項的服務集合。The DependentServices property contains the set of services that depend on this one.

如果您停止此服務相依的服務,請在父服務StopStop方法中呼叫此服務上的方法。If you stop a service that this service depends on, call the Stop method on this service within the Stop method of the parent service. ServicesDependedOn屬性包含此服務相依的服務。The ServicesDependedOn property contains the services that this service depends on.

安全性

ServiceControllerPermission
用於控制電腦上的服務。for controlling services on the computer. 相關聯的列舉:ControlAssociated enumeration: Control

SecurityPermission
適用于不受限制的服務資訊存取。for unrestricted access to service information. 相關聯的列舉:UnrestrictedAssociated enumeration: Unrestricted

適用於

另請參閱