An unhandled exception is always encountered during C # development. You can call the Appliaction. threadexcetpion () event to handle this exception.
This method is very simple to use and highly efficient. developers can easily add other functions for exception handling.
Static void main ()
{
Application. threadexception + = new threadexceptioneventhandler (application_threadexception );
Application. Run (New form1 ());
}
Private Static void application_threadexception (Object sender, threadexceptioneventargs E)
{
MessageBox. Show (E. Exception. tostring ());
}
However, this method also has limitations, that is, it can only handle unhandled exceptions in the main thread. If unhandled exceptions occur on other threads, the above method will not be able to handle them. In this case, appdomain. unhandledexception can be used to replace application. threadexception.
In this case, the exception of other working threads must still be called by the main thread. The abnormal break implementation in the working thread can be called by the main thread to determine the invokerequired attribute of the control first, when this parameter is set to true, the invoke method of the control of the main thread is called to call it.
This article is a master in the blog Park. Unfortunately, I forgot the address. Sorry, I hope the author can see it !!!