在这里,我开始一个任务:异常不能上升到主线程
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!");)
触发,那么线程不会中断。 为什么?
Proably因为计时器上一个单独的线程上运行... – animaonline 2013-02-21 12:14:40
的异常不会这样的。异常只能传播呼叫链。他们不会横着。 – 2013-02-21 12:15:42
但是,我认为如果真的需要,可以将这个异常传递给另一个线程。不是很简单,但可以完成。 – 2013-02-21 12:49:23