Professional Documents
Culture Documents
18.4 Being Notified of The Completion of An Asynchronous Delegate
18.4 Being Notified of The Completion of An Asynchronous Delegate
What if you use the ThreadPool and QueueUserWorkItem? This method will still help
you because you added the handling code that will execute inside the thread. Just
make sure you have the finally block set up so that you can notify yourself of excep-
tions in other threads as shown earlier.
In order to provide a last-chance exception handler for your WinForms application,
you need to hook up to two separate events. The first event is the System.AppDomain.
CurrentDomain.UnhandledException event, which will catch all unhandled exceptions
in the current AppDomain on worker threads; it will not catch exceptions that occur
on the main UI thread of a WinForms application. See Recipe 7.13 for more informa-
tion on the System.AppDomain.UnhandledException event. In order to catch those, you
also need to hook up to the System.Windows.Forms.Application.ThreadException,
which will catch unhandled exceptions in the main UI thread. See Recipe 7.13 for
more information about the ThreadException event.
See Also
The “Thread Class” and “Exception Class” topics in the MSDN documentation.
Solution
Use the BeginInvoke method to start the asynchronous delegate, but use the first
parameter to pass a callback delegate to the asynchronous delegate, as shown in
Example 18-3.
///////////////////////////////////////////////////////////////
// Now do it with a different type of dictionary
///////////////////////////////////////////////////////////////
// Create two observable SortedList instances
SortedList<string, bool> sortedList1 = new SortedList<string, bool>( );
SortedList<string, bool> sortedList2 = new SortedList<string, bool>( );