Imagine a WPF code-behind event handler:
<Button Click="OnButtonClick" />
In C# 4 you would declare your handler as:
private void OnButtonClick(object sender, RoutedEventArgs e) { ... }
In C# 5 you can declare an async
handler
private async void OnButtonClick(object sender, RoutedEventArgs e) { ... }
So what is WPF doing with this? A few minutes of searching about didn't turn anything up.
It seems that it's possible to perform UI updates after await
statements. Does this imply that the task is continued on the Dispatcher thread?
If the Task
raised an error, would it be raised through the WPF Dispatcher
, or only via the TaskScheduler
?
Are there any other interesting aspects to this that might be nice to understand?
The solution is extremely simple: just mark the event handler as async. You should never use async void methods, instead use async Task or async Task<T1,...>. The exception, apparently, is event handlers. And it kind of makes sense: an event handler is designed to be called asynchronously.
NET events do not support async Task as a result type! Instead, you have to cast event handlers as async void if you want to run async code inside of an event handler method and to comply with the classic . NET event delegate signature.
The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete.
You can certainly write an event handler that returns Task , but nothing else is going to call it asynchronously, directly.
You may find my async/await intro helpful.
An async
method is re-written by the compiler to support the await
operator. Every async
method starts out synchronous (in this case, on the UI thread) until it await
s some operation (that is not already completed).
By default, the context is saved, and when the operation completes, the rest of the method is scheduled to execute in that context. The "context" here is SynchronizationContext.Current
unless it is null
, in which case it is TaskScheduler.Current
. As Drew pointed out, WPF provides a DispatcherSynchronizationContext
which is tied to the WPF Dispatcher
.
Regarding error handling:
When you await
a Task
inside a WPF async void
event handler, the error handling goes like this:
Task
completes with an error. The exception is wrapped into an AggregateException
, like all Task
errors.await
operator sees that the Task
completed with an error. It unwraps the original exception and re-throws it, preserving the original stack trace.async void
method builder catches the exception escaping from an async void
method and passes it to the SynchronizationContext
that was active when the async void
method started executing (in this case, the same WPF context).AggregateException
wrapping) on the Dispatcher
.This is rather convoluted, but the intent is to have exceptions raised from async
event handlers be practically the same as exceptions raised from regular event handlers.
A partial answer. From MSDN:
An async method that has a void return type can’t be awaited, and the caller of a void-returning method can't catch any exceptions that the method throws.
So any errors would only be available via the TaskScheduler
.
Also, there's nothing XAML-specific going on with the event handler registration. It could have been done in code:
this.button.Click += OnButtonClick;
Or even as an async lambda:
this.button.Click += async (s,e) => { ... };
As for safety of UI updates after an await
, it seems that the continuation is executed within SynchronisationContext.Current
, which is set per thread. In WPF, this is a DispatcherSynchronisationContext
that's coupled to the WPF Dispatcher
that pumped the event in the first place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With