windows服务启动超时

发布于 2024-07-06 18:27:11 字数 232 浏览 6 评论 0原文

有没有办法为每个服务设置不同的服务启动超时值? 我可以使用 ServicesPipeTimeout 注册表项更改它,但它是针对每台计算机的 (http://support.microsoft.com/ kb/824344)。

目前我唯一想到的就是在不同的线程中完成所有耗时的启动操作。

Is there a way to set a different value for service startup timeout per service?
I can change it using the ServicesPipeTimeout registry key, but it's per machine (http://support.microsoft.com/kb/824344).

At the moment the only thing I thought about was to do all the time-consuming startup actions in a different thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难得心□动 2024-07-13 18:27:11

我同意罗慕洛的观点,即尽快完成为您提供服务。 但是,如果您需要时间并且使用的是 .NET Framework 2.0 或更高版本,则可以考虑 ServiceBase.RequestAdditionalTime() 方法。

http://msdn.microsoft.com/en-我们/library/system.serviceprocess.servicebase.requestadditionaltime.aspx

protected override void OnStart()
{
    this.RequestAdditionalTime(10000);
    // do your stuff
}

I agree with Romulo on finishing to start your service as soon as possible. However, if you need the time and you are using .NET Framework 2.0 or later, you might consider ServiceBase.RequestAdditionalTime() method.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.requestadditionaltime.aspx

protected override void OnStart()
{
    this.RequestAdditionalTime(10000);
    // do your stuff
}
旧时浪漫 2024-07-13 18:27:11

尽快完成服务启动是一个很好的做法。 因此,在启动状态期间,仅执行确认其成功启动所需的操作; 其余的稍后再做。 如果启动仍然是一个漫长的过程,请使用SetServiceStatus< /a> 定期通知服务控制管理器您尚未完成,因此它不会使您的服务超时。

It's good practice to finish starting your service as fast as possible. So, during the start state, do only what you absolutely need to acknowledge it started successfully; and do the rest later. If the start is still a lengthy process, use SetServiceStatus periodically to inform the Service Control Manager that you have not yet finished, so it does not time-out your service.

泛泛之交 2024-07-13 18:27:11

只需在另一个线程中执行耗时的操作

   protected override void OnStart(string[] args)
    {
        var task = new Task(() =>
        {
            // Do stuff
        });
        base.OnStart(args);
        task.Start();
    }

Simply do timeintensive stuff in another thread

   protected override void OnStart(string[] args)
    {
        var task = new Task(() =>
        {
            // Do stuff
        });
        base.OnStart(args);
        task.Start();
    }
巷雨优美回忆 2024-07-13 18:27:11

我还必须处理一项服务,该服务可能需要几秒钟/分钟才能有一个良好的开始。 当服务启动时,它会尝试连接到 SQL Server。 然而,当整个服务器重新启动时,我的服务在 SQL Server 之前启动。 (我知道服务依赖性,但由于特定原因它不适用于我的情况......)。 我尝试循环尝试连接 SQL Server 10 次,但由于超时,Windows 在第二次尝试之前终止了我的服务。

我的解决方案:我在服务的“onStart()”中添加了一个计时器。 然后,该服务的“onTick()”方法尝试连接到 SQL Server 10 次(其中等待了 30 次)。 启动时不再超时。

所以基本上,

  • 我的服务会在 5 秒内启动。
  • 服务启动后 10 秒启动计时器
    开始了。
  • 计时器尝试 10 次 [每次等待 30 秒]
    连接到 SQL Server。
  • 如果成功,计时器将自行禁用,如果失败(10 次尝试后),我将停止服务。

请注意解决问题的更优雅的方法,但也许我的解决方案的某些部分可以帮助任何比我处于相同情况的人,

I also had to deal with a service which may takes a few seconds/minutes to have a good Start. When the service starts, it tries to connect to a SQL Server. However, when the whole server was restarted , my service was starting BEFORE SQL Server. (I know about the Service dependency but it dont apply to my situation for a particular reason....). I tried to do a loop trying 10 times to connect to SQL Server, but Windows was killing my service before the 2nd try, because of the Timeout.

My solution : I added a Timer in the "onStart()" of my service. Then, the "onTick()" method of the service was trying 10 times to connect to the SQL Server (with a waiting of 30 in it). No more Timeout at startup.

So basically,

  • My service starts in 5 seconds.
  • A timer is launched 10 seconds after the service is
    started.
  • The timer tries 10 times [waiting 30 seconds each time] to
    connect to the SQL Server.
  • If it succeed, the timer will disable itself, if not (after 10 try), I stop the service.

Note the more elegant way to resolve the problem but maybe some part of my solution may help anybody in the same situation than me,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文