简体   繁体   English

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

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

Here I start a task: 在这里,我开始一个任务:

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

...and here is my worker: ...这是我的工人:

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();
}

If an exception occurs not in the timer definition but somewhere else, the exception bubbles up correctly to RefreshCacheExceptionHandling() . 如果异常不在计时器定义中而是在其他地方发生,则该异常正确地冒泡到RefreshCacheExceptionHandling() But if it fires from timer (AgregateException("Timeout!");) then the thread doesn't break. 但是,如果从计时器(AgregateException("Timeout!");)则线程不会中断。 Why? 为什么?

The System.Threading.Timer class makes callbacks on a ThreadPool thread and does not use the event model at all. System.Threading.Timer类在ThreadPool线程上进行回调,并且完全不使用事件模型。

Take a look at the documentation , here: http://msdn.microsoft.com/en-us/library/zdzx8wx8.aspx 在以下位置查看文档: http : //msdn.microsoft.com/zh-cn/library/zdzx8wx8.aspx

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

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