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

but if you call winForm like this, the style above will not affect all time, just occasionlly:

//add handler

AppDomain.CurrentDomain.UnhandledException
+=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    Application.ThreadException
+=new ThreadExceptionEventHandler(Application_ThreadException);

//initial system

Application.Run(
new Form1());

//main form

Application.Run(
new Form2());

 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?

//add handler

AppDomain.CurrentDomain.UnhandledException
+=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    Application.ThreadException
+=new ThreadExceptionEventHandler(Application_ThreadException);

//initial system

Application.Run(
new Form1());

//main form
ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(
new Form2());

 

posted on 2006-05-08 13:19  kim  阅读(1382)  评论(5编辑  收藏  举报

导航