我写了一个窗口服务在处理大量数据的C#。当我们停止尝试了一段时间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.
我写了下面code来实现它。
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);
}
}
在code启动一个任务,每10秒钟后检查任务完成与否,如果没有完成它要求额外的10秒,继续检查,直到任务完成。
The code start a Task and check after every 10 seconds if task is completed or not, if it is not completed it requests additional 10 seconds and keep checking till task completion.