在我的Windows服务中,我创建了一个“父”前台线程,它使用ThreadPool(这意味着它们是后台)产生执行任务的“子”线程。在windows服务上优雅地关闭前台线程
在Windows服务停止时优雅地关闭前台线程的最佳方式是什么?
这是我目前的执行(剥出特定任务的逻辑):
public partial class TaskScheduler : ServiceBase
{
private static AutoResetEvent _finishedTaskAutoResetEvent = new AutoResetEvent(false);
//This flag is used to increase chances of the Spawning Thread to finish gracefully when service stops.
private bool StopRequested { get; set; }
private int _executingTasksCount;
private int ExecutingTasksCount { get { return _executingTasksCount; } }
private void IncCurrentTasksCount()
{
Interlocked.Increment(ref _executingTasksCount);
}
private void DecCurrentTasksCount()
{
Interlocked.Decrement(ref _executingTasksCount);
}
public TaskScheduler()
{
InitializeComponent();
Thread spawningThread = new Thread(DoSpawnTaskExecutionThreads);
spawningThread.Name = "Spawning Thread";
spawningThread.IsBackground = false;
spawningThread.Start();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
StopRequested = true;
}
private void DoSpawnTaskExecutionThreads()
{
//We check StopRequested to try and finish this thread gracefully when service stops.
while (!StopRequested)
{
while (!StopRequested && ExecutingTasksCount < MaxPooledTasks)
{
ThreadPool.QueueUserWorkItem(ExecuteTask, new Task());
IncCurrentTasksCount();
}
_finishedTaskAutoResetEvent.WaitOne();
}
//Either all task execution threads will finish or the process will be terminated forcibly.
while (ExecutingTasksCount > 0)
{
Thread.Sleep(200); //Check five times a second.
}
_eventLog.WriteEntry("The Spawning Thread finished along with task execution threads.");
}
private void ExecuteTask(object state)
{
try
{
Task task = (Task)state;
task.Execute();
}
catch
{
// Handle exception.
}
finally
{
DecCurrentTasksCount();
_finishedTaskAutoResetEvent.Set();
}
}
}
感谢您提供如此详尽的解释和示例代码。我有一些问题: 1)“ExecutingTaskCount的检查不是线程安全的”:为什么?我只是用Interlocked类修改它。如果因为某种原因我仍然想使用它,我会怎么做呢? 你什么时候会推荐使用Interlocked类? 2)“... _finishedTaskAutoResetEvent是一个AutoResetEvent信号可能会因为WaitHandle不保持计数而丢失......”:这种情况的原因是什么?任务抛出一个未处理的异常,我因为某种原因没有处理它? – Den 2010-10-26 15:39:25
RE#1 ...为了让'ExecutingTasksCount'线程安全,您将必须执行'_executingTasksCount'的volatile读取。这可以使用'Interlocked.CompareExchange'方法或将变量标记为'volatile'来完成。 – 2010-10-26 17:58:43
RE#2 ...想象一下在DecCurrentTasksCount和_finishedTaskAutoResetEvent.Set之间所有任务线程都被抢占的假想场景(非常不可能)。我认为你的嵌套循环可以防范任何问题,但我想象的是他们可以表现的奇怪方式。再次,我认为这种方法实际上没有任何问题,但很难去思考。 – 2010-10-26 18:10:44