2011-06-09 203 views
15

我想在本地機器上停止Windows服務的服務(該服務是Topshelf.Host,如果該事項)使用此代碼:的ServiceController似乎無法停止

serviceController.Stop(); 
serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout); 

timeout設爲1小時,但服務從未實際停止。奇怪的是,從服務MMC管理單元中,我首先看到它處於「停止」狀態,但過了一段時間,它又恢復爲「已啓動」。但是,當我嘗試手動停止它時,發生錯誤:

Windows could not stop the Topshelf.Host service on Local Computer. 
Error 1061: The service cannot accept control messages at this time. 

我在這裏丟失了什麼嗎?

回答

1

我也看到過這個問題,特別是當一個服務開始等待時,我發送它停止以編程方式成功,但什麼都不做。有時候我也會看到停止命令對於正在運行的服務會失敗,但是同樣的異常仍然會停止服務。我不認爲API可以被信任去做它所說的。此錯誤消息的解釋是非常有幫助...

http://technet.microsoft.com/en-us/library/cc962384.aspx

0

而從舊的多分區框移動代碼到一個新的單個分區箱我只是打了這個問題。在服務停止時,我正在給D寫信,因爲它不再存在,所以我得到了1061錯誤。在OnStop期間的任何長操作都會導致這種情況,除非您使用回調代理將呼叫轉移到另一個線程。

10

您的服務正在忙於處理某些大型操作或正在轉換以更改狀態。因此不能接受了輸入...只是把它作爲錄取比它可以嚼...

,如果你確信你沒有什麼喂大的吧,只是去任務管理器,關閉此服務的進程或重新啓動您的機器。

1

我所面臨的類似issue.This錯誤有時會發生,因爲該服務可以不再接受控制消息,這可能是由於在該特定服務的日誌文件存在於服務器磁盤空間問題。 如果發生這種情況,您也可以考慮下面的選項。 1.轉到它的日誌文件所在的服務exe文件&的位置。 2.釋放一些空間 3.通過任務管理器殺死服務進程 4.啓動服務。

17

我知道我很晚回答這個問題,但是我遇到了類似的問題,即錯誤:「此服務無法接受控制消息。」,並希望將其添加爲其他人的參考。

您可以嘗試殺死使用PowerShell(PowerShell的運行作爲管理員)此服務:

#Get the PID of the required service with the help of the service name, say, service name. 
$ServicePID = (get-wmiobject win32_service | where { $_.name -eq 'service name'}).processID 

#Now with this PID, you can kill the service 
taskkill /f /pid $ServicePID 
+0

謝謝你,這是一個好 – user584018 2017-06-08 11:03:45

+0

使用任務管理器作爲管理員,我可以不殺該服務,重啓沒有幫助。這是爲我工作的方法。 – ChrisG 2017-08-18 11:36:17

+0

爲我工作。不要忘記以管理員身份運行PowerShell。 – Aaron 2017-10-13 23:03:56

1

我有完全一樣的問題Topshelf託管服務。原因是服務啓動時間長,超過20秒。這使服務處於無法處理進一步請求的狀態。 我能夠重現只有當問題被服務從命令行(NET START my_service)開始。

與長星時間Topshelf服務正確初始化以下內容:

namespace Example.My.Service 
{ 
    using System; 
    using System.Threading.Tasks; 

    using Topshelf; 

    internal class Program 
    { 
     public static void Main() 
     { 
      HostFactory.Run(
       x => 
       { 
        x.Service<MyService>(
         s => 
         { 
          MyService testServerService = null; 
          s.ConstructUsing(name => testServerService = new MyService()); 
          s.WhenStarted(service => service.Start()); 
          s.WhenStopped(service => service.Stop()); 
          s.AfterStartingService(
           context => 
           { 
            if (testServerService == null) 
            { 
             throw new InvalidOperationException("Service not created yet."); 
            } 
            testServerService.AfterStart(context); 
           }); 
         }); 
        x.SetServiceName("my_service"); 
       }); 
     } 
    } 

    public sealed class MyService 
    { 
     private Task starting; 

     public void Start() 
     { 
      this.starting = Task.Run(() => InitializeService()); 
     } 

     private void InitializeService() 
     { 
      // TODO: Provide service initialization code. 
     } 

     [CLSCompliant(false)] 
     public void AfterStart(HostControl hostStartedContext) 
     { 
      if (hostStartedContext == null) 
      { 
       throw new ArgumentNullException(nameof(hostStartedContext)); 
      } 
      if (this.starting == null) 
      { 
       throw new InvalidOperationException("Service start was not initiated."); 
      } 
      while (!this.starting.Wait(TimeSpan.FromSeconds(7))) 
      { 
       hostStartedContext.RequestAdditionalTime(TimeSpan.FromSeconds(10)); 
      } 
     } 

     public void Stop() 
     { 
      // TODO: Provide service shutdown code. 
     } 
    } 
}