来自线程的异常不';不要冒泡到主线

本文关键字:线程 异常 | 更新日期: 2023-09-27 18:00:20

在这里我开始一个任务:

System.Threading.Tasks.Task.Factory.StartNew( () =>
    RefreshCache(crmClientInfo)).ContinueWith(
        previous => RefreshCacheExceptionHandling(previous.Exception),
        TaskContinuationOptions.OnlyOnFaulted
    );

这是我的工人:

public void RefreshCache(CrmClientInfo _crmClientInfo)
{
    AsyncManager.OutstandingOperations.Increment();
    Timer timer = new Timer(10000);
    timer.Elapsed += delegate
    {
        throw new AggregateException("Timeout!");
    };
    timer.Start();
    OtherServices.RefreshCache(crmClientInfo);
    AsyncManager.OutstandingOperations.Decrement();
}

如果异常不是发生在计时器定义中,而是发生在其他地方,则异常会正确地冒泡到RefreshCacheExceptionHandling()。但是如果它从定时器(AgregateException("Timeout!");)启动,那么线程就不会中断为什么

来自线程的异常不';不要冒泡到主线

系统。线程。Timer类对ThreadPool进行回调线程,并且根本不使用事件模型。

请查看以下文档:http://msdn.microsoft.com/en-us/library/zdzx8wx8.aspx