Catch UnHanle Exception in WinForm
as we know, we can catch unhanle exception like this:
AppDomain.CurrentDomain.UnhandledException+=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException+=new ThreadExceptionEventHandler(Application_ThreadException);
//add handler
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
AppDomain.CurrentDomain.UnhandledException+=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException+=new ThreadExceptionEventHandler(Application_ThreadException);
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
//initial system
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
Application.Run(new Form1());
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
//main form
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
Application.Run(new Form2());
//add handler
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
AppDomain.CurrentDomain.UnhandledException+=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException+=new ThreadExceptionEventHandler(Application_ThreadException);
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
//initial system
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
Application.Run(new Form1());
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
//main form
ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(new Form2());
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
but if you call winForm like this, the style above will not affect all time, just occasionlly:
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
since when Application.Run, it begins one Message Loop in current main thread, and the thread exception's hook work.
but when Form1 close, the thread hook release, but then we begin the other message loop, the exception's hook release,
so if we hook it twice, what happen?
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)
![](http://10.16.50.19:8989/Images/OutliningIndicators/None.gif)