Windows 服务启动(超时(30000 毫秒))



我已经在vb.net中为我的项目创建了一个Windows服务安装程序文件,但是在Windows服务安装后,当我尝试启动该Windows服务时,它会抛出以下错误:

The service did not respond to the start or control request in a timely fashion. 
Timeout (30000 milliseconds) waiting for the Test service to connect.

我能做什么?

在 Windows 服务中,启动时间限制为 30 秒。如果该服务在 30 秒后未开始响应 ServiceControlManager 调用,则会终止。

这通常是因为您在 OnStart() 方法中放置了一些长时间运行的代码,这些代码无法及时完成。

尝试在Service Constructor和OnStart方法中保留最少的代码,然后如果需要执行其他任何操作,例如调用数据库,加载数据,IO,调用外部服务等,都应在主应用程序中完成。

看看你的服务OnStart()方法?你在那里做任何繁重的工作吗?如果某些事情花费的时间比预期的要长,不会引发异常?

看起来好像你想做这样的事情

class MyService
{
    public void OnStart()
    {
        //blocks here
        Thread.Sleep(TimeSpan.FromSeconds(31));     
    }
}

相反,你应该做一些类似的事情

class MyService
{
    private Thread workerThread;
    public void OnStart()
    {
        workerThread = new Thread(()=>
        {
            Thread.Sleep(TimeSpan.FromSeconds(31));     
        })
        // doesn't block here
        workerThread.Start();
    }
}

最新更新