Why is async void bad and how do I await a Task in an object constructor in C#?
In this video, I answer the question "Why is async void bad and how do I await a Task in an object constructor?".
Async void is generally considered bad for 3 reasons:
1. You can’t wait for its completion (fire and forget)
2. Any unhandled exceptions will terminate your process (can't be caught)
3. Difficult to test (see #1 and #2)
Async void methods have different error-handling semantics. When an exception is thrown out of an async Task method, that exception is captured and placed on the Task object. With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started.
However, let's be honest, there are many areas of our code where an async void will be required. The scenario that immediately come to mind is in the constructor of an object. For example, maybe you want to load data async when your object is first created. You want to load your data async, and let the object creation process continue without blocking any threads. This is normally done with what's called a "fire and forget" approach. Other scenarios may include the Execute method of an ICommand, or within an event handler.
Your first instinct may be to wrap these async/await calls into an "async void" method, and then just call that method in your constructor, command, or event handler. But, this is not recommended.
Luckily for use, we can use the power of a C# extension method to provide support for not only handling the completion of a Task in an async void scenario, but also handle any exceptions that may occur and provide better unit testing support.
Check out my new Pluralsight course "Introduction to Prism for WPF":
https://pluralsight.pxf.io/bE3rB
Sponsor Me:
https://github.com/sponsors/brianlagunas
Follow Me:
Twitter: https://twitter.com/brianlagunas
Twitch: https://www.twitch.tv/brianlagunas
Blog: http://brianlagunas.com
GitHub: https://github.com/brianlagunas
Видео Why is async void bad and how do I await a Task in an object constructor in C#? канала Brian Lagunas
Async void is generally considered bad for 3 reasons:
1. You can’t wait for its completion (fire and forget)
2. Any unhandled exceptions will terminate your process (can't be caught)
3. Difficult to test (see #1 and #2)
Async void methods have different error-handling semantics. When an exception is thrown out of an async Task method, that exception is captured and placed on the Task object. With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started.
However, let's be honest, there are many areas of our code where an async void will be required. The scenario that immediately come to mind is in the constructor of an object. For example, maybe you want to load data async when your object is first created. You want to load your data async, and let the object creation process continue without blocking any threads. This is normally done with what's called a "fire and forget" approach. Other scenarios may include the Execute method of an ICommand, or within an event handler.
Your first instinct may be to wrap these async/await calls into an "async void" method, and then just call that method in your constructor, command, or event handler. But, this is not recommended.
Luckily for use, we can use the power of a C# extension method to provide support for not only handling the completion of a Task in an async void scenario, but also handle any exceptions that may occur and provide better unit testing support.
Check out my new Pluralsight course "Introduction to Prism for WPF":
https://pluralsight.pxf.io/bE3rB
Sponsor Me:
https://github.com/sponsors/brianlagunas
Follow Me:
Twitter: https://twitter.com/brianlagunas
Twitch: https://www.twitch.tv/brianlagunas
Blog: http://brianlagunas.com
GitHub: https://github.com/brianlagunas
Видео Why is async void bad and how do I await a Task in an object constructor in C#? канала Brian Lagunas
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![Which do I use, ConfigureAwait True or False?](https://i.ytimg.com/vi/F9_8MJbsnzg/default.jpg)
![C# Yield Return: What is it and how does it work?](https://i.ytimg.com/vi/HRXkeaeImGs/default.jpg)
![How to Close Windows from a ViewModel in C#](https://i.ytimg.com/vi/U7Qclpe2joo/default.jpg)
![5 Things You're Doing Wrong When Programming in Python](https://i.ytimg.com/vi/fMRzuwlqfzs/default.jpg)
![How do I Navigate GitHub Code?](https://i.ytimg.com/vi/V1yvdQszBDM/default.jpg)
![Best Practices - Async / Await | The Xamarin Show](https://i.ytimg.com/vi/-LY4ATA8Bgw/default.jpg)
![How to Report Progress with Async/Await in .NET Core 3](https://i.ytimg.com/vi/zQMNFEz5IVU/default.jpg)
![How to Delete Rows from a Data Grid](https://i.ytimg.com/vi/IRE2PAD1kIM/default.jpg)
![C# Advanced Async - Getting progress reports, cancelling tasks, and more](https://i.ytimg.com/vi/ZTKGRJy5P2M/default.jpg)
![Python tricks: Demystifying async, await, and asyncio](https://i.ytimg.com/vi/tSLDcRkgTsY/default.jpg)
![Task vs ValueTask: When Should I use ValueTask?](https://i.ytimg.com/vi/dCj7-KvaIJ0/default.jpg)
![Prism Region Not Found Exception in WPF](https://i.ytimg.com/vi/Y7P5T19uLtw/default.jpg)
![ContinueWith: Solving async void (response to Brian Lagunas) - .NET ADVANCED ESSENTIALS](https://i.ytimg.com/vi/vYXs--S0Xxo/default.jpg)
![What is TPL ( Task Parallel Library) and how it differs from threads (c# interview questions) ?](https://i.ytimg.com/vi/No7QqSc5cl8/default.jpg)
![Supporting IAsyncEnumerable with LINQ](https://i.ytimg.com/vi/Ktl8K2b1-WU/default.jpg)
![MVVM 101 - Model-View-ViewModel Architecture for Xamarin.Forms (also WPF, UWP, & More)](https://i.ytimg.com/vi/Pso1MeX_HvI/default.jpg)
![How to Cancel Tasks in C# - Using CancellationTokenSource and CancellationToken](https://i.ytimg.com/vi/TKc5A3exKBQ/default.jpg)
![8 await async mistakes that you SHOULD avoid in .NET](https://i.ytimg.com/vi/lQu-eBIIh-w/default.jpg)
![Understanding how to use Task and ValueTask](https://i.ytimg.com/vi/fj-LVS8hqIE/default.jpg)
![Debugging Managed Async Code in Visual Studio 2019](https://i.ytimg.com/vi/aVEug50YpaM/default.jpg)