当自己定义的线程出现异常,怎样通知给主线程,static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {            MessageBox.Show(DateTime.Now.ToString());
        }
进入到这个方法进行统一的处理啊???????????????????

解决方案 »

  1.   

    是的  一般都是采用统一处理
    Application.ThreadException
    AppDomain.CurrentDomain.UnhandledException
      

  2.   

    在Run之前加上
    Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
      

  3.   

    private void Form1_Load(object sender, EventArgs e)
            {
                //throw new Exception();
                LoadForm();
                #if DEBUG
               // MessageBox.Show(DateTime.Now.Minute.ToString());
                #endif
            }
            private void LoadForm()
            {
                
                //try
                //{
                //    int i = Convert.ToInt32("asd");
                //}
                //catch (Exception e)
                //{            //    throw new Exception(e.Message);
                //}
                Thread td = new Thread(new ThreadStart(start));
                td.Start();
            }        private void start()
            {
                throw new Exception();
            }static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {            MessageBox.Show(DateTime.Now.ToString());
            }
    不知道怎么进不来啊????????
      

  4.   

    private void Form1_Load(object sender, EventArgs e)
            {
                //throw new Exception();
                LoadForm();
                #if DEBUG
               // MessageBox.Show(DateTime.Now.Minute.ToString());
                #endif
            }
            private void LoadForm()
            {
                
                //try
                //{
                //    int i = Convert.ToInt32("asd");
                //}
                //catch (Exception e)
                //{            //    throw new Exception(e.Message);
                //}
                Thread td = new Thread(new ThreadStart(start));
                td.Start();
            }        private void start()
            {
                throw new Exception();
            }static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
            {            MessageBox.Show(DateTime.Now.ToString());
            }
    不知道怎么进不来啊????????
      

  5.   

    在程序的Program类的Main方法里面添加如下代码
    #if !DEBUG
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
              
    #endif同时再添加两个方法处理异常信息! static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
            {
                MessageBox.Show(e.Exception.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }        static void UnhandledExceptionFunction(Object sender, UnhandledExceptionEventArgs args)
            {
                MessageBox.Show(((Exception)args.ExceptionObject).Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }其它主要处理子线程错误的就只有这两行
      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
      AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);
    以及UnhandledExceptionFunction方法这样就可以捕捉到子线程的异常了