Restored original synhcornization context before exiting
MODIFY
src/Chapter20/Listing20.08.CatchingExceptionFromAsyncVoidMethod.cs
src/Chapter20/Listing20.08.CatchingExceptionFromAsyncVoidMethod.cs
@@ -56,15 +56,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_08
public static void Main()
{
-
+ SynchronizationContext? originalSynchronizationContext =
+ SynchronizationContext.Current;
+ try
+ {
AsyncSynchronizationContext synchronizationContext =
new AsyncSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(
synchronizationContext);
- try
- {
-
OnEvent(typeof(Program), new EventArgs());
#if WithOutUsingResetEvent
@@ -86,6 +86,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20.Listing20_08
Console.WriteLine($@"{exception} thrown as expected.(Thread ID: {
Thread.CurrentThread.ManagedThreadId})");
}
+ finally
+ {
+ SynchronizationContext.SetSynchronizationContext(
+ originalSynchronizationContext);
+ }
}
static async void OnEvent(object sender, EventArgs eventArgs)

Restored original synhcornization context before exiting