线程c#中发生异常时,进程未终止 [英] Process not terminated when exception occured in thread c#

查看:631
本文介绍了线程c#中发生异常时,进程未终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

/感谢

在下面运行代码dem:

I have a System.Timers.Timer and I found out that in the ElapsedEventHandler an exception sometimes occured but the process did *not* die but the thread executing the ElapsedEventHandler died quitly where exception occured. Not good since I then wasn''t aware about the exception. Why isn''t the process terminated? How could I get the process terminated in best way in case an exception occurs in the ElapsedEventHandler?

I tried similar using a System.Threading.Timer and then the process terminated. Can you explain the difference?

/Thanks

Running code dem below:

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");
    }
}

推荐答案

昨天我遇到类似问题时正在研究这个问题.如果您打开已发布的源代码Timer.cs [已发生的事件中[^ ] (请参阅备注部分).我想知道有关此行为在框架的更高版本中可能会更改"的评论是否能避免他们想改正错误!

艾伦.
I was looking into this yesterday when I had a similar problem. If you open the published source code Timer.cs[^] and scroll down to the MyTimerCallback method you''ll see that the event invocation has an empty catch block.

Although I had never noticed it before, the behaviour is documented Elapsed Event[^] (see the remarks section). I wonder if the comment about this behaviour being "subject to change in later versions of the framework" is a get out in case they ever want to fix their mistake!

Alan.


这篇关于线程c#中发生异常时,进程未终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆