IBackgroundThreadExceptionCatcher Interface |
Namespace: VirtualRadar.Interface
The IBackgroundThreadExceptionCatcher type exposes the following members.
Name | Description | |
---|---|---|
ExceptionCaught |
Raised when an exception is caught on the background thread. The background thread should not
pass ThreadAbortException through this.
|
Many interfaces need to perform tasks on a background thread. In general an exception on a background thread will end up going to the unhandled exception handler, which will log it and show it to the user. However leaving an exception on a background thread unhandled can cause problems, particularly if the exception is raised by an event handler. It may prevent other event handlers from seeing that event.
In many cases the background thread would like to catch exceptions and have them displayed to the user on the GUI thread, or logged by the normal unhandled exception handler, without having to actually get involved in how this might be done.
The idea here is that interfaces whose implementation may involve work on a background thread will include this interface. If the implementation does use background threads then they should catch exceptions raised on those threads and raise an ExceptionCaught event in response. Some other bit of code will then take over and do whatever is necessary to report or log the exception.
Background threads will usually be sent a ThreadAbortException when the thread is shut down - they should not send those through ExceptionCaught.
public event EventHandler<EventArgs<Exception>> ExceptionCaught; protected virtual void OnExceptionCaught(EventArgs<Exception> args) { if(ExceptionCaught != null) ExceptionCaught(this, args); } private void BackgroundThreadMethod() { try { // Do some work here... } catch(ThreadAbortException) { // This is raised if the thread is shutting down, you don't need to pass these on to // the user. Whatever you do in this catch block it will always end with the framework // throwing another ThreadAbort at the end of the catch. } catch(Exception ex) { OnExceptionCaught(new EventArgs<Exception>(ex)); } }