【问题标题】:Windows Service won't stop/startWindows 服务不会停止/启动
【发布时间】:2017-01-30 23:07:42
【问题描述】:

我正在使用以下代码片段来停止服务。但是,两个Console.Writeline 语句都表明服务正在运行。为什么服务不会停止?

class Program
{
    static void Main(string[] args)
    {
        string serviceName = "DummyService";
        string username = ".\\Service_Test2";
        string password = "Password1";

        ServiceController sc = new ServiceController(serviceName);

        Console.WriteLine(sc.Status.ToString());

        if (sc.Status == ServiceControllerStatus.Running)
        {
            sc.Stop();
        }

        Console.WriteLine(sc.Status.ToString());
    }
}

【问题讨论】:

  • 你在哪里使用用户名和密码??
  • 我在代码中进一步使用它,在其中更改与服务关联的帐户和密码。

标签: c#


【解决方案1】:

您需要调用sc.Refresh() 来刷新状态。请参阅http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.stop.aspx 了解更多信息。

此外,服务可能需要一些时间才能停止。如果该方法立即返回,则将您的关闭更改为如下内容可能会很有用:

// Maximum of 30 seconds.

for (int i = 0; i < 30; i++)
{
    sc.Refresh();

    if (sc.Status.Equals(ServiceControllerStatus.Stopped))
        break;

    System.Threading.Thread.Sleep(1000);
}

【讨论】:

    【解决方案2】:

    在检查状态之前调用 sc.Refresh()。也可能需要一些时间才能停止。

    【讨论】:

      【解决方案3】:

      尝试调用:

      sc.Refresh();
      

      在您致电 Status 之前。

      【讨论】:

        【解决方案4】:

        我相信你应该使用 sc.stop 然后刷新 http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.refresh(VS.80).aspx

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

        【讨论】:

          【解决方案5】:

          尝试以下方法:

           while (sc.Status != ServiceControllerStatus.Stopped)
           {
               Thread.Sleep(1000);
               sc.Refresh();
           }
          

          【讨论】:

            【解决方案6】:

            您是否拥有停止/启动服务的正确权利?

            您以什么帐户运行您的控制台应用程序?管理员权限?

            您是否尝试过 sc.WaitForStatus?可能是服务正在停止,但在您到达 writeline 时尚未停止。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-04-05
              • 1970-01-01
              相关资源
              最近更新 更多