这里我开始一个任务:
System.Threading.Tasks.Task.Factory.StartNew( () =>
RefreshCache(crmClientInfo)).ContinueWith(
previous => RefreshCacheExceptionHandling(previous.Exception),
TaskContinuationOptions.OnlyOnFaulted
);
...这是我的 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();
}
如果异常不是发生在计时器定义中而是发生在其他地方,则异常会正确地冒泡到 RefreshCacheExceptionHandling()
。但是,如果它从计时器 (AgregateException("Timeout!");)
触发,则线程不会中断。 为什么?
最佳答案
The System.Threading.Timer class makes callbacks on a ThreadPool thread and does not use the event model at all.
查看文档,此处:http://msdn.microsoft.com/en-us/library/zdzx8wx8.aspx
关于c# - 来自线程的异常不会冒泡到主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15001979/