ServiceController.Start 方法

定义

启动服务。

重载

Start()

启动服务,不传递任何自变量。

Start(String[])

启动服务,传递指定的参数。

Start()

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
ServiceController.cs

启动服务,不传递任何自变量。

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

例外

访问 API 时出错。

未找到服务。

示例

以下示例使用 ServiceController 类检查警报器服务是否已停止。 如果服务已停止,则示例将启动服务并等待服务状态设置为 Running

// Check whether the Alerter service is started.
ServiceController^ sc = gcnew ServiceController;
if ( sc )
{
   sc->ServiceName =  "Alerter";
   Console::WriteLine(  "The Alerter service status is currently set to {0}", sc->Status );
   if ( sc->Status == (ServiceControllerStatus::Stopped) )
   {
      // Start the service if the current status is stopped.
      Console::WriteLine(  "Starting the Alerter service..." );
      try
      {
         // Start the service, and wait until its status is "Running".
         sc->Start();
         sc->WaitForStatus( ServiceControllerStatus::Running );
         
         // Display the current service status.
         Console::WriteLine(  "The Alerter service status is now set to {0}.", sc->Status );
      }
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine(  "Could not start the Alerter service." );
      }
   }
}

// Check whether the Alerter service is started.

ServiceController sc  = new ServiceController();
sc.ServiceName = "Alerter";
Console.WriteLine("The Alerter service status is currently set to {0}",
                   sc.Status);

if (sc.Status == ServiceControllerStatus.Stopped)
{
   // Start the service if the current status is stopped.

   Console.WriteLine("Starting the Alerter service...");
   try
   {
      // Start the service, and wait until its status is "Running".
      sc.Start();
      sc.WaitForStatus(ServiceControllerStatus.Running);

      // Display the current service status.
      Console.WriteLine("The Alerter service status is now set to {0}.",
                         sc.Status);
   }
   catch (InvalidOperationException)
   {
      Console.WriteLine("Could not start the Alerter service.");
   }
}

' Check whether the Alerter service is started.

Dim sc As New ServiceController()
sc.ServiceName = "Alerter"
Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status)

If sc.Status = ServiceControllerStatus.Stopped Then
   ' Start the service if the current status is stopped.
   Console.WriteLine("Starting the Alerter service...")

   Try
      ' Start the service, and wait until its status is "Running".
      sc.Start()
      sc.WaitForStatus(ServiceControllerStatus.Running)
      
      ' Display the current service status.
      Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status)
   Catch 
      Console.WriteLine("Could not start the Alerter service.")
   End Try
End If

注解

在服务控制器状态为 Running之前,无法为服务调用 Stop

另请参阅

适用于

Start(String[])

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
ServiceController.cs

启动服务,传递指定的参数。

public:
 void Start(cli::array <System::String ^> ^ args);
public void Start (string[] args);
member this.Start : string[] -> unit
Public Sub Start (args As String())

参数

args
String[]

在服务启动时传递给它的自变量数组。

例外

访问 API 时出错。

服务无法启动。

argsnull

- 或 -

该数组的一个成员是 null

注解

在服务控制器状态为 Running之前,无法为服务调用 Stop

另请参阅

适用于