Professional Documents
Culture Documents
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>( );
Discussion
This recipe contains two similar but distinct methods. The first method,
FindOccurrenceOf, returns a particular occurrence of a regular expression match. The
occurrence you want to find is passed in to this method via the occurrence parame-
ter. If the particular occurrence of the match does not exist—for example, you ask to
find the second occurrence, but only one occurrence exists—a null is returned from
this method. Because of this, you should check that the returned object of this
method is not null before using that object. If the particular occurrence exists, the
Match object that holds the match information for that occurrence is returned.
The second method in this recipe, FindEachOccurrenceOf, works similarly to the
FindOccurrenceOf method, except that it continues to find a particular occurrence of
a regular expression match until the end of the string is reached. For example, if you
ask to find the second occurrence, this method would return a List<Match> of zero or
more Match objects. The Match objects would correspond to the second, fourth, sixth,
and eighth occurrences of a match and so on until the end of the string is reached.
See Also
The “.NET Framework Regular Expressions” and “ArrayList Class” topics in the
MSDN documentation.
Now, anytime the letter a is encountered, no matter what its case, the Split method
views that character as a delimiter.
See Also
The “String.Join Method” topic in the MSDN documentation.
Solution
C# provides two methods for iterating strings. The first is the foreach loop, which
can be used as follows:
string testStr = "abc123";
foreach (char c in testStr)
{
Console.WriteLine(c.ToString( ));
}
This method is quick and easy. Unfortunately, it is somewhat less flexible than the
second method, which uses the for loop instead of a foreach loop to iterate over the
string. For example:
string testStr = "abc123";
for (int counter = 0; counter < testStr.Length; counter++)
{
Console.WriteLine(testStr[counter]);
}
Discussion
The foreach loop is simpler and thus less error-prone, but it lacks flexibility. In
contrast, the for loop is slightly more complex, but it makes up for that in flexibility.
The for loop method uses the indexer of the string variable testStr to get the
character located at the position indicated by the counter loop index. Care must be
taken not to run over the bounds of the string array when using this type of looping
mechanism.