代码如下
        private void button1_Click(object sender, EventArgs e)
        {
            BeginThreadX();
        }        private Thread thread;
        public void BeginThreadX()
        {
            thread = new Thread(ExcpThread);
            thread.Start();        }        public void ExcpThread()
        {
            string i = "helloworld";
            throw new Exception("thread excp");
            
        }点击按钮之后开启一个线程,在线程中抛出异常
同时在main函数中已做捕捉
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());            Application.ThreadException -= new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);        }这时候虽然有捕捉到异常,可还是一样会程序崩溃(ctrl+f5)运行下
如果F5调试,就是个不断抛异常的循环过程,这个太奇妙了网上都说只要ThreadException ,UnhandledException 抓下就行了,实际好像不行
求教大家看看

解决方案 »

  1.   

    这个问题应该是你创建的thread throw exception, 但你没有处理,clr会将这个exception抛给process,process中也没有处理,所以,最后process直接退出。
    一般,写多线程,都应该处理exception。
    例如:public void BeginThreadX()
            {
                Task<string> task = new Task<string>(ExcpThread);            task.ContinueWith(tempTask => 
                {
                    try
                    {
                        MessageBox.Show(tempTask.Result);
                    }
                    catch (AggregateException ex)
                    {
                        MessageBox.Show("Got error");
                    }
                });            task.Start();
            }        public string ExcpThread()
            {
                this.textBox2.Text = "test";
                string i = "helloworld";
                throw new Exception("thread excp");
            }
      

  2.   


    楼上同学,继续求教一下
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);我现在在CurrentDomain_UnhandledException这个函数里捕获到这个异常了,你说要怎么处理才能让程序不崩溃。我觉得catch到了应该就是处理的意思,即使catch里什么都不写,不知道同学你说的处理是指?
      

  3.   

    看看这个。
    http://stackoverflow.com/questions/4284986/catching-unhandled-exception-on-separate-threads