Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Never rethrow an exception at the highest point in the exception-han-

dling hierarchy within a thread. Since no exception handlers can catch


this rethrown exception, it will be considered unhandled, and the
thread will terminate after all finally blocks have been executed.

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.

18.4 Being Notified of the Completion of an


Asynchronous Delegate
Problem
You need a way of receiving notification from an asynchronously invoked delegate
that it has finished. This scheme must allow your code to continue processing with-
out having to constantly call IsCompleted in a loop or to rely on the WaitOne method.
Since the asynchronous delegate will return a value, you must be able to pass this
return value back to the invoking thread.

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.

Being Notified of the Completion of an Asynchronous Delegate | 727


Example 9-6. Using the ObservableDictionary and ObservableDictionaryObserver
classes (continued)
// hook up the approval events for adding or changing
observer.ApproveAdd +=
new ObservableDictionaryObserver<int, string>.
Approval(SeekApproval);
observer.ApproveChange +=
new ObservableDictionaryObserver<int, string>.
Approval(SeekApproval);

// Use the observable instances


obsDict1.Add(1, "one");
obsDict2.Add(2, "two");
obsDict3.Add(3, "three");

// Insure the approval process worked


Debug.Assert(obsDict1.Count == 1);
Debug.Assert(obsDict2.Count == 1);
// this should be empty as the value was more than three characters
Debug.Assert(obsDict3.Count == 0);

// Unregister the observable instances


observer.Unregister(obsDict3);
observer.Unregister(obsDict2);
observer.Unregister(obsDict1);

///////////////////////////////////////////////////////////////
// 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>( );

var obsSortedList1 = sortedList1.MakeObservableDictionary( );


var obsSortedList2 = sortedList2.MakeObservableDictionary( );

// Create an observer for the two subject objects


ObservableDictionaryObserver<string, bool> listObserver =
new ObservableDictionaryObserver<string, bool>( );

// Register the three subjects with the observer


listObserver.Register(obsSortedList1);
listObserver.Register(obsSortedList2);

// hook up the approval events for adding or changing


listObserver.ApproveAdd +=
new ObservableDictionaryObserver<string, bool>.
Approval(ApprovePositive);
listObserver.ApproveChange +=
new ObservableDictionaryObserver<string, bool>.
Approval(ApprovePositive);

// Use the observable instances

342 | Chapter 9: Delegates, Events, and Lambda Expressions

You might also like