To enhance the robustness of the currently developing system, you need to capture unexpected and unhandled exceptions during the runtime. After checking the information, find The basic steps for the application. threadexception event to handle these exceptions include,
1. Add a function handle to handle the threadexception event.
2. Define a function to handle exceptions
Example:
[stathread]
static void main () {application. threadexception + = New threadexceptioneventhandler (application_threadexception); application. run ( New frmmain ();} private static void application_threadexception ( Object sender, threadexceptioneventargs e) {MessageBox. show ( "unhandled exception:" + E. exception. tostring () ;}
This method is easy to use and has a high processing efficiency. You can easily add other functions to handle exceptions based on your intentions. However, I found that if a third-party control does not work at all, the third-party control may have run in different threads. As mentioned in Microsoft's help, the above method can only handle exceptions not handled in the main thread. Okay, check the Internet, find the following method, and use Appdomain. unhandledexception replaces application. threadexception.
First, you need to understand that the event handler function defined at this time needs to be executed in the thread that throws an exception, but the exception prompt in the main thread is completed in the main thread, how can this problem be solved? The followingCodeA complete solution is provided.
Private Delegate Void Exceptiondelegate (exception X );
Static Private Frmmain _ mainform;
/// <Summary>
/// The main entry point for the application.
/// </Summary>
[Stathread]
Static Void Main ()
{
_ Mainform = New Frmmain ();
Appdomain. currentdomain. unhandledexception + = New Unhandledexceptioneventhandler (appdomain_unhandledexception );
Application. Run (_ mainform );
}
Private Static Void Appdomain_unhandledexception ( Object Sender, unhandledexceptioneventargs E)
{
Exception exception;
Exception = E. exceptionobject As Exception;
If (Exception =Null )
{
// This is an unmanaged exception, you may want to handle it differently
Return ;
}
Publishonmainthread (exception );
}
Private Static Void Publishonmainthread (exception)
{
If (_ Mainform. invokerequired)
{
// Invoke executes a delegate on the thread that owns _ mainforms's underlying window handle.
_ Mainform. Invoke ( New Exceptiondelegate (handleexception ), New Object [] {Exception });
}
Else
{
Handleexception (exception );
}
}
Private Static Void Handleexception (exception)
{
If (Systeminformation. userinteractive)
{
Using (Threadexceptiondialog dialog = New Threadexceptiondialog (exception ))
{
If (Dialog. showdialog () = dialogresult. Cancel)
Return ;
}
Application. Exit ();
Environment. Exit (0 );
}
}
Private Void Threadmethod ()
{
Throw New Exception ( "From New thread" );
}
Private Void Button#click ( Object Sender, system. eventargs E)
{
Thread thread;
Thread = New Thread ( New Threadstart (threadmethod ));
Thread. Start ();
}
Note that:
1. All AppdomainOf UnhandledexceptionAdd a processing
2, The unhandledexceptioneventargs parameter contains Isterminating attribute, indicating whether to abort Common Language Runtime