Click or drag to resize
IBackgroundThreadExceptionCatcher Interface
An interface for objects that may be utilising background threads and want to allow exceptions on those threads to bubble up to the GUI thread.

Namespace: VirtualRadar.Interface
Assembly: VirtualRadar.Interface (in VirtualRadar.Interface.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public interface IBackgroundThreadExceptionCatcher

The IBackgroundThreadExceptionCatcher type exposes the following members.

Events
  NameDescription
Public eventExceptionCaught
Raised when an exception is caught on the background thread. The background thread should not pass ThreadAbortException through this.
Top
Remarks

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.

Examples
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));
    }
}
See Also