.net - 为什么计时器线程上未处理的异常不会使进程崩溃

标签 .net multithreading c#-4.0 asynchronous unhandled-exception

我知道使用 Task 时如何处理未处理的异常s,如果用户代码还没有“观察到”它,只会在终结器中抛出一个未处理的。

我也知道异步线程中未处理的异常(例如 Action.BeginInvoke() )是如何在加入调用(例如 Action.EndInvoke() )中被捕获并重新抛出的。

我不明白的是这如何不会使过程崩溃?

    static void Main(string[] args)
    {
        var timer = new System.Timers.Timer() {Interval = 100};
        timer.Elapsed += (o, e) => { throw new Exception(); };
        timer.Start();

        Console.ReadKey( true );
    }

最佳答案

来自 .NET 4.0 文档:

In the .NET Framework version 2.0 and earlier, the Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.



http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

目前还没有声明声称这种行为实际上已经改变。

关于.net - 为什么计时器线程上未处理的异常不会使进程崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9407062/

相关文章:

java - 线程停止执行

c++ - 使用epoll_wait时如何正确读取数据

c# - 一个大数据库查询比许多小数据库查询有什么优势

xml - 如何使用 xml 文档在现有元素下方添加新元素

c# - 在 C# 中将两个 List<> 相交

c# - 从属性构造函数内部获取应用了哪个属性的成员?

c# - .NET 微基准测试 API

java - Java 中用于蜂窝自动化的线程安全高性能矩阵式容器?

c# - 如何捕获自定义文件夹上的 Outlook 约会删除事件?

.net - 在我的 WPF 项目安装程序中,如何捆绑 .NET3.5 安装程序?