[英]Error 1053: Service did not respond in time
I created a new Windows-Service project and added it to the services using sc.exe, but I am always getting the error when I try to execute the Service.我创建了一个新的 Windows-Service 项目并使用 sc.exe 将其添加到服务中,但是当我尝试执行该服务时总是出现错误。
Code in Program:程序中的代码:
static void Main() {
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
Code in ServiceBase: ServiceBase中的代码:
public Service1() {
InitializeComponent();
}
protected override void OnStart(string[] args) {
while(true) {
Console.WriteLine("Message all 5 sec...");
Thread.Sleep(5000);
}
}
protected override void OnStop() {
Environment.Exit(0);
}
I tried extending the Timeout in Registry(ServicesPipeTimeout), Using Threads and owning the Service but i still get the error.我尝试在注册表 (ServicesPipeTimeout) 中延长超时,使用线程并拥有该服务,但我仍然收到错误。
Any Help is appreciated.任何帮助表示赞赏。
Kind Regards亲切的问候
Your service will never get out of the onStart-callback because of the endless loop you have created there.由于您在那里创建的无限循环,您的服务将永远不会脱离 onStart-callback。 So this is blocking and will never finish.
所以这是阻塞的,永远不会完成。
You need to use a timer for your use-case.您需要为您的用例使用计时器。 Just start a timer in your OnStart method and it shall run as expected:
只需在您的 OnStart 方法中启动一个计时器,它就会按预期运行:
public Service1() {
InitializeComponent();
}
public OnStart(string[] args)
{
Timer timer = new Timer();
timer.Interval = 5000; // 5 seconds
timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
timer.Start();
}
public void OnTimer(object sender, ElapsedEventArgs args)
{
Console.WriteLine("Message all 5 sec...");
}
The timer will send an event every 5 seconds and the added ElapsedEventHandler will call your OnTimer-Method.计时器将每 5 秒发送一个事件,添加的 ElapsedEventHandler 将调用您的 OnTimer-Method。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.