Correcting Common Async/Await Mistakes in .NET - Brandon Minnick

NDC Conferences · 171K views · 2019 · 1:00:11

Open on YouTube

Software practice, open source & careersC, C++, Rust, Go & systems languagestalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about async void, lost exceptions, blocking calls and ConfigureAwait, as a go-to explanation of how async/await really works in .NET.

Recommended by 5 people across 7 different months, 2019 to 2021.

What it is

A conference talk from NDC by Brandon Minnick on how the .NET compiler turns async methods into state machine classes and how that hides exceptions and costs performance. He takes an existing app and fixes its async code live, showing gains in exception handling, runtime speed and app size.

What people said

Strong rec MindSwipe · 2020-07 · Stack Overflow · on “UI Blocking in Async method”

Async void is *always* bad. Because you cannot await a function that doesn't return a task. Meaning exceptions can (and will) be lost. I highly recommend watching Brandon Minnick's talk about async/ await [this video] . Instead of async void, use `async Task`. The only exception should be `EventHandlers`, as documented by MS themselves here [LINK]

Recommends Stephan · 2019-07 · Stack Overflow · on “Microsoft Graph API timeout exception”

First of all if it's an application with user interaction you should never do blocking ( .Wait() or .Result ) calls on the main thread, this will lock-up your application for further user interaction. So you need to start by using async code, this is a sample of an async Button_Click handler. It will give you a warning, but this seems to be one of the use-cases of an async void. private async void Button_Click(object sender, RoutedEventArgs { var currentUser = await graphClient.Me.Request().GetAsync(); // Presumably do something with the response labelUser.Text = currentUser.DisplayName; } Few days ago I found a nice video [this video] about common async/await mistakes You seem to be talking abou

Recommends MindSwipe · 2019-11 · Stack Overflow · on “Task is asynchronous or synchronous without async await?”

The SO post is talking about the `Wait()` method on the Task, if it is called the calling Thread will wait for the execution to be done. If not then the calling thread will move on (but do not call `Wait()` manually, you should always await Tasks). Here are 2 videos that helped me understand async/ await: 1. [LINK] 2. [this video]

Recommends Brandon Minnick · 2020-01 · Stack Overflow · on “Navigation Stack Destroyed after onResume()?”

You're just missing the async + await keywords. If you're interested in learning why we need the async and await keywords, I recommend watching this presentation I recently gave on async/await, explaining how it works in .NET: [this video] protected override async void OnResume() { base.OnResume(); try { MakeWebRequest(); if (IsConnected != true) { MainPage = new NoInternet(); } else { await MainPage.Navigation.PopToRootAsync(true); await MainPage.Navigation.PushAsync(new Home("XXXX")); } } catch (IOException ex) { Crashes.TrackError(ex); } }

Recommends Yoda · 2020-09 · Stack Overflow · on “Async command execution in MVVM light”

You're _really_ doing it wrong. Neither is using `DispatcherHelper` recommended, nor `try/catch` needed, nor even one of them suggested. I _strongly_ suggest to you to read @Stephen Cleary's answer & comments above and/or watch at least [this video] on how to do stuff like this.

Recommends Yoda · 2021-04 · Stack Overflow · on “How to build a modern, synchronized ObservableCollection?”

@StephenCleary: because after watching Brandon Minnick's [this video] about async/await best practices, I thought doing `await service.GetAsync().ConfigureAwait(false)` is the way to go wherever possible to exactly do that. If that's wrong, is there some "more complete" example/tutorial I couldn't find so far that you know of & can point me to?

Recommends Yom B · 2021-10 · Stack Overflow · on “How to catch exception in an async method?”

Catching AggregateException will be possible only if the task is awaited. So when you do not await the Task.Run, the executing thread moves out of the try block and now you can't catch the exception in case it occurs. As for the AggregateException - it is a wrapper which holds one or more exceptions which you can access inside its property InnerExceptions. I suggest this fantastic read for in depth about this topic and threading in C# in general: [LINK] Also you check this out: [this video] (around 26:00 is the answer, but a great talk in general!)