c# - 来自线程的异常不会冒泡到主线程

标签 c# .net multithreading exception

这里我开始一个任务:

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/

相关文章:

ios - 出现错误,因为在执行 [FMDatabaseQueue inDatabase :]? 之后至少有一个打开的结果集

c# - 使用 visual studio 和 entity framework core 5.0.2 连接到 MSSQL Server developer edition

c# - 获取字符串 C# 中第一个非字母字符的索引

c# - 使用 System.WIndows.Forms.WebBrowser 控件进行打印

.net - 在 .NET 中表示不可变列表的最佳方式是什么?

c++ - 在没有事件异常的情况下调用线程终止

c# - 从cs页面禁用输入类型单选按钮单击并使用js向用户发送警报?

c# - 是否有理由以这种方式进行类型比较?

.net - 如何将 .net 标准 2.0 安装到 Visual Studio 2017 中

c - c中的pthread条件等待