Windows服务等待停止请求的最长时间是多少,以及如何请求额外的时间 [英] what is the maximum time windows service wait to process stop request and how to request for additional time

查看:158
本文介绍了Windows服务等待停止请求的最长时间是多少,以及如何请求额外的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#编写了一个Windows服务,该服务可以处理大量数据。当我们停止它时,尝试尝试20/30秒,然后引发异常。

I have written a windows service in c# that process a lot data. when we stop it try for sometime 20/30 seconds and then throws exception.

我想在OnStop事件中实现ServiceBase.RequestAdditionalTime()。

I want to implement ServiceBase.RequestAdditionalTime() in OnStop event.

我想知道Windows服务引发异常之后的确切超时,以便我可以在它之前请求额外的时间。

I want to know the exact timeout after which windows service throws the exception, so that I can request additional time just before it.

我进行了搜索,但没有找到该默认的停止超时值。

I searched but did not find this default stop timeout value.

推荐答案

我编写了以下代码来实现这一目标。

I wrote the following code to achieve it.

protected override void OnStop()
{
  int timeout = 10000;
  var task = Task.Factory.StartNew(() => MyTask());
  while (!task.Wait(timeout))
  {
      RequestAdditionalTime(timeout);
  }
}

上面的代码以与主程序平行的方式启动任务线程(任务立即开始运行),下一行是每10秒检查一次任务是否完成,如果未完成,则请求额外的10秒,并继续检查直到任务完成。

The above code starts a Task in Parallel to the main thread (Task start running immediately), next line is to check if task is completed or not every 10 seconds and if it is not completed it requests additional 10 seconds and keep checking till task get completed.

这篇关于Windows服务等待停止请求的最长时间是多少,以及如何请求额外的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆