I explain how async void
methods work - and why they should be avoided - in my Best Practices in Asynchronous Programming article.
async void
has the same semantics as async Task
, except for exceptions. An async void
method will capture the current SynchronizationContext
at the beginning of the method, and any exceptions from that method will be captured and raised directly on that captured context. In the most common scenarios, this will cause an application-level exception, usually a crash. Some people call async void
methods "fire-and-forget", but because of their exceptional behavior, I prefer "fire-and-crash". :)
"Avoid async void" is the general guideline, with one notable exception: event handlers (or items that are logically event handlers, such as ICommand.Execute
implementations).
How does it work? How does this translate to aspnet?
It works just like any other async
method. The main platform difference is that the UI thread doesn't need to know when the async
method completes. ASP.NET needs to know that so it knows when to send the request, but the UI has no need to know when the async
method completes. So async void
works. It's still best avoided, because the calling code usually does need to know when it completes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…