简体   繁体   English

错误1053:服务没有及时响应

[英]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.

相关问题 错误 1053 服务没有及时响应启动或控制请求 - Error 1053 the service did not respond to the start or control request in a timely fashion 错误 1053 服务没有响应启动或控制请求 - Error 1053 the service did not respond to the start or control request 错误1053:服务未及时响应启动或控制请求 - Error 1053: the service did not respond to the start or control request in a timely fashion 错误1053:服务没有响应 C#.Net Core 3.1 - Error 1053: Service did not respond C# .Net Core 3.1 错误1053,服务没有及时响应请求 - Error 1053. The service did not respond to the request in a timely manner 1053 windows服务没有及时响应 - 1053 windows service did not respond in timely fashion Topshelf 服务不会作为服务启动:错误 1053 服务没有响应..” - Topshelf service wont start as a service: Error 1053 The service did not respond.." 使用 TopShelf 创建 Windows 服务时出现“错误 1053 服务没有响应”错误 - "Error 1053 The Service did not respond" error when using TopShelf to create a Windows Service 启动服务:“错误1053:服务未及时响应启动或控制请求” - Starting a service: “Error 1053: The service did not respond to the start or control request in a timely fashion” Windows服务无法启动'错误1053:服务未及时响应启动或控制请求' - Windows Service won't start 'Error 1053: The service did not respond to the start or control request in timely fashion'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM