繁体   English   中英

线程异常不会冒泡到主线程

[英]Exception from thread doesn't bubble up to main thread

在这里,我开始一个任务:

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!");)则线程不会中断。 为什么?

System.Threading.Timer类在ThreadPool线程上进行回调,并且完全不使用事件模型。

在以下位置查看文档: http : //msdn.microsoft.com/zh-cn/library/zdzx8wx8.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM