简体   繁体   中英

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() . But if it fires from timer (AgregateException("Timeout!");) then the thread doesn't break. Why?

The System.Threading.Timer class makes callbacks on a ThreadPool thread and does not use the event model at all.

Take a look at the documentation , here: http://msdn.microsoft.com/en-us/library/zdzx8wx8.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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