所以我试图运行我编程的服务,由于某种原因,它给我这个错误,当我尝试启动它:开发服务,错误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