所以我試圖運行我編程的服務,由於某種原因,它給我這個錯誤,當我嘗試啓動它:開發服務,錯誤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()作爲一個獨立的應用程序,沒有例外。
你必須啓動一個線程http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx你在onStart,而不是睡覺 – rene 2012-08-06 13:46:04
事情是我必須每隔1小時運行一次線程,我怎麼能這樣做,而不會讓主進程進入睡眠狀態,因爲使用Timer會佔用更多的資源並且效率不高,如何告訴服務器必須運行每隔一小時穿線? @rene – 2012-08-06 13:52:26
@ gerald-versluis – 2012-08-06 13:53:08