【发布时间】:2014-09-10 01:03:09
【问题描述】:
我有两种方法:
public void Stop(bool immediate)
{
Logger.Log(LogLevel.Debug, "Shutdown detected. Immediate: " + immediate);
Shutdown();
Logger.Log(LogLevel.Debug, "Unregistering");
HostingEnvironment.UnregisterObject(this);
}
public void Shutdown()
{
Logger.Log(LogLevel.Debug, "Preparing to stop UploadQueue");
IsProcessing = false;
//Set tasks to cancel to prevent queued tasks from parsing
_cancellationTokenSource.Cancel();
Logger.Log(LogLevel.Debug, "Waiting for " + _workerTasks.Count + " tasks to finish or cancel.");
//Wait for tasks to finish
Task.WaitAll(_workerTasks.Values.ToArray());
Logger.Log(LogLevel.Debug, "Stopped UploadQueue");
}
该类正在使用 IRegisteredObject 接口来接收关闭通知。在我的日志中,我得到了这个:
2014-07-18 15:30:55,913,DEBUG,Shutdown detected. Immediate: False
2014-07-18 15:30:55,913,DEBUG,Preparing to stop UploadQueue
2014-07-18 15:30:55,913,DEBUG,Waiting for 35 tasks to finish or cancel.
...
bunch of stuff
...
2014-07-18 15:31:28,471,DEBUG,Shutdown detected. Immediate: True
2014-07-18 15:31:28,471,DEBUG,Preparing to stop UploadQueue
2014-07-18 15:31:28,471,DEBUG,Waiting for 0 tasks to finish or cancel.
2014-07-18 15:31:28,471,DEBUG,Stopped UploadQueue
2014-07-18 15:31:28,471,DEBUG,Unregistering
为什么不是第一次到达Logger.Log(LogLevel.Debug, "Stopped UploadQueue");?它似乎确实取消了任务并让正在运行的任务完成。 (任务在运行前检查它是否被取消,否则就是这样)。
【问题讨论】:
-
也许某些任务在第一种情况下会抛出异常?所以 Logger.Log() 方法不是这样调用的。
标签: c# asp.net iis application-pool