问题描述
当自己定义的线程出现异常,怎样通知给主线程,staticvoidApplication_ThreadException(objectsender,System.Threading.ThreadExceptionEventArgse){MessageBox.Show(DateTime.Now.ToString());}进入到这个方法进行统一的处理啊???????????????????
解决方案
解决方案二:
是的一般都是采用统一处理Application.ThreadExceptionAppDomain.CurrentDomain.UnhandledException
解决方案三:
在Run之前加上Application.ThreadException+=newThreadExceptionEventHandler(Application_ThreadException);
解决方案四:
privatevoidForm1_Load(objectsender,EventArgse){//thrownewException();LoadForm();#ifDEBUG//MessageBox.Show(DateTime.Now.Minute.ToString());#endif}privatevoidLoadForm(){//try//{//inti=Convert.ToInt32("asd");//}//catch(Exceptione)//{//thrownewException(e.Message);//}Threadtd=newThread(newThreadStart(start));td.Start();}privatevoidstart(){thrownewException();}staticvoidApplication_ThreadException(objectsender,System.Threading.ThreadExceptionEventArgse){MessageBox.Show(DateTime.Now.ToString());}不知道怎么进不来啊????????
解决方案五:
privatevoidForm1_Load(objectsender,EventArgse){//thrownewException();LoadForm();#ifDEBUG//MessageBox.Show(DateTime.Now.Minute.ToString());#endif}privatevoidLoadForm(){//try//{//inti=Convert.ToInt32("asd");//}//catch(Exceptione)//{//thrownewException(e.Message);//}Threadtd=newThread(newThreadStart(start));td.Start();}privatevoidstart(){thrownewException();}staticvoidApplication_ThreadException(objectsender,System.Threading.ThreadExceptionEventArgse){MessageBox.Show(DateTime.Now.ToString());}不知道怎么进不来啊????????
解决方案六:
在程序的Program类的Main方法里面添加如下代码#if!DEBUGApplication.ThreadException+=newThreadExceptionEventHandler(Application_ThreadException);#endif同时再添加两个方法处理异常信息!staticvoidApplication_ThreadException(objectsender,ThreadExceptionEventArgse){MessageBox.Show(e.Exception.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}staticvoidUnhandledExceptionFunction(Objectsender,UnhandledExceptionEventArgsargs){MessageBox.Show(((Exception)args.ExceptionObject).Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}其它主要处理子线程错误的就只有这两行Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);AppDomain.CurrentDomain.UnhandledException+=newUnhandledExceptionEventHandler(UnhandledExceptionFunction);以及UnhandledExceptionFunction方法这样就可以捕捉到子线程的异常了