Skip to content
0:00 / 0:00

#asyncvoid#asyncawait#task

Stop using async void in C#! Do this instead.

Nick Chapsas

429K subscribers • 63 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • Presented by Nick Chapsas

Intermediate. Assumes working knowledge.

61K views3 years ago2 recommendations

Programming languagesSoftware designC# and .NETConcurrency and parallelism

Nick Chapsas explains the dangers of using async void in C# and demonstrates how to handle asynchronous fire-and-forget operations safely.

Use code REST15 for 15% off the new From Zero to Hero - REST APIs in .NET course: bit.ly/restchapsas Become a Patreon and get source code access: patreon.com/nickchapsas Hello everybody I'm Nick and in this video I will show you why you should not be using async void in C#. It is a very common mistake that can have very catastrophic effects for your application and in this video I will explain why. Blog by Gérald Barré: meziantou.net/fire-and-forget-a-task-in-dotnet.htm Workshops: bit.ly/nickworkshops Don't forget to comment, like and subscribe :) Social Media: Follow me on GitHub: bit.ly/ChapsasGitHub Follow me on Twitter: bit.ly/ChapsasTwitter Connect on LinkedIn: bit.ly/ChapsasLinkedIn Keep coding merch: keepcoding.shop #csharp #dotnet

Why it's here

2 people recommend this

What people say. Recommended as a helpful explanation of why async void is dangerous in C# and how to handle exceptions properly.

Moments people linked to

u/The_MAZZTerin r/dotnet • Recommends

On “[HELP REQUEST] Taking user input to send email using synchronous/asynchronous tasks”

…re though. 2\. You are calling an async function (sendEmail) from a synchronous function (the event handler). You should always handle the returned Task from an async function or weird things happen with Exceptions you might not expect (though they make sense with how async is implemented). This video covers in a bit more detail why this is bad: link to this video When you can do is declare the event handler async void. (Yes that video says not to do that; it is ok in scenarios where it's unavoidable if you keep in mind the consequences) so you can then await on sendEmail. You can't declare event handlers async Task since the standard signature has a return value of void. So for compatibility reason…

u/Kraigiusin r/programming • Recommends

On “I started a repo to gather a collection of scripts that leverage programing language quirks that cause unexpected behavior. It's just so much fun to see the wheels turning in someone's head when you show them a script like this. Please send in a PR if you feel like you have a great example!”

…ored in the returned task. The exception is rethrown when the task is awaited. Make sure that any async method that can produce an exception has a return type of Task or Task<TResult> and that calls to the method are awaited. It kill the software if an exception is thrown in a method that is Task<void>. If you prefer a video that talk about it: link to this video…
1View on Reddit