2012-08-06 102 views
0

所以我試圖運行我編程的服務,由於某種原因,它給我這個錯誤,當我嘗試啓動它:開發服務,錯誤1053

Error 1053: the service did not respond to the start or control request in a timely fashion 

我的代碼是非常基本。

static void Main(string[] args) 
    { 
     try 
     { 
      ServiceBase[] ServicesToRun; 
      ServicesToRun = new ServiceBase[] 
      { 
       new Program() 
      }; 
      ServiceBase.Run(ServicesToRun); 
     } 
     catch (Exception ex) 
     { 
      string SourceName = "WindowsService.ExceptionLog"; 
      if (!EventLog.SourceExists(SourceName)) 
      { 
       EventLog.CreateEventSource(SourceName, "Application"); 
      } 

      EventLog eventLog = new EventLog(); 
      eventLog.Source = SourceName; 
      string message = string.Format("Exception: {0} \n\nStack: {1}", ex.Message, ex.StackTrace); 
      eventLog.WriteEntry(message, EventLogEntryType.Error); 
     } 
    } 

    public Program() 
    { 
     this.ServiceName = "FetchFeed"; 
    } 

    protected override void OnStart(string[] args) 
    { 
     base.OnStart(args); 

     //TODO: place your start code here 
    repeat: FetchFeed(); 
     Thread.Sleep(3600000); 
     goto repeat; 
    } 

    protected override void OnStop() 
    { 
     base.OnStop(); 

     //TODO: clean up any variables and stop any threads 
    } 

private static void FetchFeed() 
{ 
    //Some HTTP requests and retrieval. 
} 

,這是安裝程序類:

[RunInstaller(true)] 
public class Service_Installer : Installer 
{ 
    public Service_Installer() 
    { 
     ServiceProcessInstaller processInstaller = new ServiceProcessInstaller(); 
     ServiceInstaller serviceInstaller = new ServiceInstaller(); 

     //set the privileges 
     processInstaller.Account = ServiceAccount.LocalSystem; 

     serviceInstaller.DisplayName = "FetchFeed"; 
     serviceInstaller.StartType = ServiceStartMode.Manual; 

     //must be the same as what was set in Program's constructor 
     serviceInstaller.ServiceName = "FetchFeed"; 

     this.Installers.Add(processInstaller); 
     this.Installers.Add(serviceInstaller); 
    } 
} 

可能是什麼錯誤背後的原因是什麼?我已經檢查過FetchFeed()作爲一個獨立的應用程序,沒有例外。

+0

你必須啓動一個線程http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx你在onStart,而不是睡覺 – rene 2012-08-06 13:46:04

+0

事情是我必須每隔1小時運行一次線程,我怎麼能這樣做,而不會讓主進程進入睡眠狀態,因爲使用Timer會佔用更多的資源並且效率不高,如何告訴服務器必須運行每隔一小時穿線? @rene – 2012-08-06 13:52:26

+0

@ gerald-versluis – 2012-08-06 13:53:08

回答

1

使用appopiate timer

public class YourService 
{ 
     var tim = new System.Timers.Timer(60 * 60 * 1000); // 1 hour 

    protected override void OnStart(string[] args) 
    { 
     base.OnStart(args); 

     tim.AutoReset = true; 
     tim.Elapsed += new System.Timers.ElapsedEventHandler(tim_Elapsed); 
     tim.Enabled = true; 
     // first time run 
     ThreadPool.QueueUserWorkItem(WaitCallback(FetchFeed)); 
    } 

    static void tim_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
      FetchFeed(); 
    } 
} 
+0

所以如何確保FetchFeed()第一次運行而不需要等待1小時?@rene – 2012-08-06 14:33:49

+0

如果FetchFeed在30秒內保證有效,您可以立即調用它。您可以使用第二個計時器,該計時器在autoreset = false秒後運行,或使用ThreadPool排列工作項。我在我的答案中做了最後一個選項。 – rene 2012-08-06 14:42:05

1
repeat: FetchFeed(); 
Thread.Sleep(3600000); 
goto repeat; 

這就是你的問題。 Windows服務應在30秒內響應,因此Windows知道已成功啓動。

使用此代碼,您將阻止唯一的線程,並且服務不再響應任何事情。

試着用不同的線程或其他方式做這件事。

我建議另一種方式,因爲使用goto是一個真正不走:)

還檢查了在Windows服務的一些教程和基本的文檔,以獲得更好的理解。

相關問題