.net - 线程C#中发生异常时,进程未终止

标签 .net multithreading exception process terminate

我有一个System.Timers.Timer,我发现在ElapsedEventHandler中有时会发生异常,但进程并未死掉,但是执行ElapsedEventHandler的线程在发生异常的地方会死掉。不好,因为那时我还不知道该异常。为什么流程没有终止?如果ElapsedEventHandler中发生异常,如何最好地终止进程?

我尝试使用System.Threading.Timer进行类似操作,然后进程终止。
你能解释一下区别吗?

完整的代码如下:

class Program
{
    static public void DoBad()
    {
        Console.WriteLine("DoBad enter");
        string s = "hello";
        object o = s;
        int i = (int)o; // Ops! Something unexpected happend here... Exception
        Console.WriteLine("DoBad exit");
    }

    static void DoIt1(object sender, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("\nDoIt1 enter");
        DoBad(); // Process is NOT terminated when excpetion occurs in DoBad, thread just dies quietly but process continues. Why is process not terminated?
        Console.WriteLine("DoIt1 exit");
    }

    static void Test1()
    {
        System.Timers.Timer t = new System.Timers.Timer(2000);
        t.Elapsed += new System.Timers.ElapsedEventHandler(DoIt1);
        t.Enabled = true;
    }

    static public void DoIt2(Object o)
    {
        Console.WriteLine("\nDoIt2 enter");
        DoBad(); // Process terminated when exception occurs in DoBad.
        Console.WriteLine("DoIt2 exit");
    }

    static void Test2()           
    {
        System.Threading.Timer timer = new System.Threading.Timer(DoIt2, null, 0, 2000);
    }

    static void Main(string[] args)
    {
        Test1(); // Process NOT terminated when exception occurs in DoBad
        // Test2(); // Process terminated when exception occurs in DoBad

        // Why is process NOT terminated when running Test1 (but terminated when running Test2)?
        // Want process to die. How to do it in best way?

        Console.WriteLine("Main Sleep");
        System.Threading.Thread.Sleep(11000);
        Console.WriteLine("Main Slept");
    }
}

/谢谢!

最佳答案

这是设计使然,如您在此处看到的:(system.timers.timer)
在.NET Framework 2.0版及更早版本中,Timer组件捕获并抑制事件处理程序为Elapsed事件抛出的所有异常。此行为可能会在.NET Framework的将来版本中更改。

system.threading.timer不会执行此操作,因此异常被传递出去并终止进程。
如果您想了解该异常,则应捕获该异常并将其记录下来。
如果要在主线程中完成所有工作,则可以使用invoke/begininvoke:

form1.Invoke((ThreadStart)delegate
                {
                //do something
                });

或使用Windows.forms.timer(winforms)/System.Windows.Threading.DispatcherTimer(WPF)

关于.net - 线程C#中发生异常时,进程未终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868197/

相关文章:

.net - 如何在派生类上动态调用静态方法

c# - 如何创建具有多种尺寸/图像的 System.Drawing.Icon?

c# - WCF 流式下载 - IIS 与控制台中托管的 CPU 利用率较高

python - 捕获除用户中止之外的所有异常

c# - 在 WPF 中很好地控制缩放和添加元素

java - 多线程 : Map Performance

c# - 进行大量 PropertyUpdates 时避免 UI 锁定

java - 登录多线程环境

硬编码参数时的 Java 异常处理

mongodb - 蒙戈异常