【发布时间】:2013-02-21 12:12:10
【问题描述】:
我在这里开始一个任务:
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!");) 触发,那么线程不会中断。 为什么?
【问题讨论】:
-
可能是因为计时器在单独的线程上运行...
-
异常不能这样工作。异常只能向上传播调用链。他们不会横着走。
-
但是,如果确实需要,我认为可以将此异常传递给另一个线程。不是很简单,但可以做到。
标签: c# .net multithreading exception exception-handling