QueueBackgroundWorkItem CancellationToken未按预期工作

时间:2016-02-01 16:12:38

标签: c# asp.net multithreading .net-4.5.2

我有一个进程设置为在后台线程上执行。我正在尝试确保我们正确处理IIS进程正常关闭,但CancellationToken似乎没有工作。为了测试,我在后台线程运行的代码中有这个:

    while (!cancellationToken.IsCancellationRequested)
        System.Threading.Thread.Sleep(1000);

    cancellationToken.ThrowIfCancellationRequested();

...

catch (System.OperationCanceledException ex)
{
    string message = String.Format("The background thread cancelled while copying a page because the application was shutting down. " +
                                            "The page may have been copied to some locations prior to cancellation, but the task did not complete. RelativeUrl: {0}", relativeUrl);

    _logger.Error(ex, message);
    ...
}

问题是我的日志永远不会获取日志条目。我已经尝试在包含ThrowIfCancellationRequested的行上使用断点连接调试器,并在catch块中添加一个断点,然后回收应用程序池。我的断点从未被击中,工作进程结束,VS调试器分离。我是否误解或误用了取消令牌?

修改

以下是我开始任务的方式:

HostingEnvironment.QueueBackgroundWorkItem(c => processor.CopyPage(relativeUrl, overwrite, subPages, c));

我将上面的代码更新为此并再次尝试:

while (true)
{
    System.Threading.Thread.Sleep(1000);
    cancellationToken.ThrowIfCancellationRequested();
}

这次我表演了iisreset。启动iisreset后,我继续在VS调试器中单步执行循环。 IsCancellationRequested仍然是假的,并且似乎从未出现异常。有趣的是CancellationToken.CanBeCancelled设置为false。此属性的文档说明:

  

如果CanBeCanceled返回false,则保证令牌永远不会转换为已取消状态,这意味着IsCancellationRequested永远不会返回true。

0 个答案:

没有答案
相关问题