On 15 Oct, 21:03, Mark_B <Ma...@discussions.microsoft.comwrote:
I have a usb device that controls a power supply. That usb device must
perform a procedure, named Zero_outputs(), in the event of any error.
Clearly, within my main form (the only form in this application) I can have
each method envelope a Try-Catch statement that calls Zero_outputs() in the
catch clause.
However, I fear that certain application errors might occur outside of the
context of any given method in my main form. Is there a way to execute
Zero_outputs() whenever any error, of any kind, occurs in my application?
Use Application.ThreadException to catch exceptions in all forms
threads.
Please read carefully:
http://msdn.microsoft.com/en-us/libr...exception.aspx
NOTE (from the above link):
To use this event, you must attach a handler before you call
Application..::.Run.
If an exception occurs in the main application thread and no catch
block in the call stack handles it, the default exception handler
catches it and terminates the application. If an exception occurs in a
thread other than the main application thread, the thread exits, but
the application continues to run.
AND, ALSO IMPORTANT:
Because this is a static event, you must detach your event handlers
when your application is disposed, or memory leaks will result.
Hope it helps.