Correcting Common Async/Await Mistakes in .NET 8 - Brandon Minnick - NDC London 2024

  Рет қаралды 19,954

NDC Conferences

NDC Conferences

Ай бұрын

This talk was recorded at NDC London in London, England. #ndclondon #ndcconferences #developer #softwaredeveloper
Attend the next NDC conference near you:
ndcconferences.com
ndclondon.com/
Subscribe to our KZbin channel and learn every day:
/@NDC
Follow our Social Media!
/ ndcconferences
/ ndc_conferences
/ ndc_conferences
#dotnet #csharp #code #aws #concurrency
Did you know that the .NET compiler turns our async methods into classes? And that .NET adds a try/catch block to each of these classes, potentially hiding thrown exceptions? It's true!
In this session, we will learn how to best use async/await in C# by analyzing how .NET compiles our async code.
Join me as we take an existing app and optimize its async code together, showing off performance gains, better exception handling, improved run-time speed, and smaller app size!

Пікірлер: 33
@berkanbilgin2287
@berkanbilgin2287 Ай бұрын
THIS WAS AWESOME.. RIGHT ON POINT. NOT TOO LONG, NOT TOO SHORT. GREAT JOB, THANKS..
@MaQy
@MaQy Ай бұрын
The part about FrozenSet and FrozenDictionary is a misconception. Those two collections were not added because they are immutable, you already have ImmutableHashSet and ImmutableDictionary. You should keep using those in most circumstances. The Frozen ones are meant to be used when they are are created on startup, kept in memory, and repeatedly used afterwards. The creation cost is way higher, but the reading part is significantly faster, so they are not suitable for operations that create them on the fly and discard them after they are finished.
@gregh2327
@gregh2327 Ай бұрын
I didn't want to watch the whole video to see if Brandon Minnick changed anything, but I've seen an similar video from him over the years with the same misguided info. Here's the last one; my complaints are the top comment: kzbin.info/www/bejne/sJmmg4tpd5ykrbs. Wish I had this guys confidence in being wishy-washy with the details.
@brandonminnick
@brandonminnick 23 күн бұрын
Totally fair feedback. You're absolutely right - there is a higher cost to create `FrozenSet` than `IReadOnlyList` and `ImmutableList`. When running benchmarks on my app, I found that the additional performance cost was around 1ms. For this app, it's not much of a performance hit; for example, I'm not dropping any frames due to using `FrozenSet`. That being said, I appreciate the feedback and I will update my recommendations for using `FrozenSet` to include this caveat 💯
@xaviar7247
@xaviar7247 Ай бұрын
Awesome presentation. Thanks for showing the iasynenumerable idea . I saw it somewhere, used it, lost the code, and couldn't find it again. Gonna go back to using this. HERO!
@brandonminnick
@brandonminnick 23 күн бұрын
Thanks! You're most welcome!
@49riddickful
@49riddickful Ай бұрын
Great voice, great tempo and super captivating. Fantastic listen
@brandonminnick
@brandonminnick 23 күн бұрын
Thanks so much!
@clystian
@clystian 14 күн бұрын
46:36 Async/Await Best Practices
@tugayersoy4489
@tugayersoy4489 Ай бұрын
it was cool, ty
@jinparksoul
@jinparksoul Ай бұрын
It doesn't feel right to put work in the constructor. Especially when you need to create a lot of view models at once for say a list . I usually have some other type of async Task method to initialize work it so I or my team don't need to remember some extension method I made a year ago to handle doing work in the constructor safely. You also have to be very careful with using ConfigureAwait(false) before a collection clear. The ViewModel should have little to no knowledge of what or how the UI is bound to it. A UI team member may bind a list control to the Collection that is being cleared here. However if you add a ConfigureAwait(false) in the call before and the execution continues in a thread that is not the UI thread when the collection clear occurs it will fire change notification events in a different thread from the UI. This will crash the app as the control bound to the collection cannot be modified from a non-ui thread.
@brandonminnick
@brandonminnick 23 күн бұрын
Agreed. In the .NET MAUI apps I've published to the app stores, I trigger this logic using the `OnAppearing()` method. But, for this talk it's a quick + dirty example to segueway into the pros + cons of `async void`.
@LZE.
@LZE. Ай бұрын
Great talk!
@brandonminnick
@brandonminnick 23 күн бұрын
Thanks!!
@woutervugt
@woutervugt Ай бұрын
Great talk. Quick question though. Why would you do work in a constructor at all? Constructors initialize memory, methods do logic. Most coding platforms I worked with (winforms, wpf, asp,etc) have a 'starting' method or event that you can use so that your constructor doesn't need any weird 'safe way to execute async void code'. For instance, even though you created this safe approach to async void, you still cannot handle the error in a meaningful way (eg, showing it to the user). So, instead you choose the only left option which is to trace it out. Why would you do it in that way?
@djupstaten2328
@djupstaten2328 Ай бұрын
Some like the "init" qualifier for properties because they allow for flexibility while retaining safety. Unfortunately that initialization can't be deferred to an init-method (unless you call it in the constructor). Its also a good thing to have the object in the expected state whenever a check for "is not null" or "is " returns true, and there's a potential opening between ctor --> init-method for any such checks to occur, where some properties may not have been initiated. That would open the can of worms of locking etc.
@TheRPGminer
@TheRPGminer Ай бұрын
​@@djupstaten2328But why can't we have private constructor, and have public async factory method?
@newbiex11
@newbiex11 Ай бұрын
@@djupstaten2328 yes why don't just use Onload / Init method?
@berkanbilgin2287
@berkanbilgin2287 Ай бұрын
@@TheRPGminer you can, but this more of a self triggerred/invoked operations where you just creata an instance from a factory and it kicks in
@brandonminnick
@brandonminnick 23 күн бұрын
Yup - agreed, I don't recommend triggering any async code in the constructor. In the .NET MAUI apps I publish to the app stores, I trigger this logic using the `OnAppearing()` method, not in the constructor. But, for this talk it's a quick + dirty example to segueway into the pros + cons of `async void`. To handle the exception using `.SafeFireForget()`, you can pass in an Excecption Handler: `.SafeFireAndForget(ex => Trace.WriteLine(ex))`
@garcipat
@garcipat Ай бұрын
For the issue with intelisense there is a recommendation to suffix async methods with Async. Not my favorit but in this case it would help
@alexaka1
@alexaka1 Ай бұрын
In reality .net doesn't do all that thread switching. That is actually a misunderstanding that is being spread by others. When you await a Task, nothing actually happens, yes the state machine is generated, but there is no thread switch. The calling thread continues to execute the internals of the task synchronously. The only time a thread switch happens is when an actually async method is called. Seems counter intuitive but try it out. There are some fake async methods in .Net that don't make a thread switch, and there are real async methods like File IO that do actually yield. This behavior causes the same misconception about Task.Run(), ppl think that they forced a thread switch when in fact they didn't. Put an infinite loop inside Task.Run() and then don't await it. You'll see that your program halts, even though it shouldn't. Counter intuitive, but that's how it is. It is interesing how the ForceYield flag changes this, but without it, it will not actually switch threads in most cases.
@alfflasymphonyx
@alfflasymphonyx Ай бұрын
this feels like the same video as this one: kzbin.info/www/bejne/sJmmg4tpd5ykrbs
@mrqbboy
@mrqbboy Ай бұрын
Again?
@gregh2327
@gregh2327 Ай бұрын
Is it still inaccurate?
@brandonminnick
@brandonminnick 23 күн бұрын
@@gregh2327 If there's any innacuraces in the video, please do let me know! I've iterated this talk over the last 8 years and have certainly made improvements based on the feedback I receive. It's never my intention to mislead anyone.
@gregh2327
@gregh2327 22 күн бұрын
@@brandonminnick Thank you for responding and being open. I unfortunately don't have time to watch and do a write up; but a couple comments in here are astute. In previous talks, my frustration has been regarding discrepancies in how the CLR works and how you describe it working.
@garcipat
@garcipat Ай бұрын
ConfigureAwait being true by default was a dumb desicion.
@gregh2327
@gregh2327 22 күн бұрын
I initially disagreed, but now I'm on the fence. I think you'd find an equal number of people that disagree.
@kahnfatman
@kahnfatman Ай бұрын
The whole .NET is a mistake ... let alone async
Is it concurrent or parallel?
3:48
Jacob Sorber
Рет қаралды 17 М.
Chips evolution !! 😔😔
00:23
Tibo InShape
Рет қаралды 39 МЛН
顔面水槽がブサイク過ぎるwwwww
00:58
はじめしゃちょー(hajime)
Рет қаралды 123 МЛН
Uma Ki Super Power To Dekho 😂
00:15
Uma Bai
Рет қаралды 60 МЛН
Brutally honest advice for new .NET Web Developers
7:19
Ted's Tech
Рет қаралды 24 М.
Tune Jo Na Kaha
5:10
Mohit Chauhan
Рет қаралды 35 МЛН
Back to Basics: Efficient Async and Await - Filip Ekberg - NDC Oslo 2023
1:01:25
What is C#? What is the difference between C# and .NET?
6:33
Interview Happy
Рет қаралды 61 М.
Await Async Tasks Are Getting Awesome in .NET 9!
9:24
Nick Chapsas
Рет қаралды 84 М.
How Neuralink Works 🧠
0:28
Zack D. Films
Рет қаралды 31 МЛН
Apple watch hidden camera
0:34
_vector_
Рет қаралды 47 МЛН
Обманет ли МЕНЯ компьютерный мастер?
20:48
Харчевников
Рет қаралды 186 М.
3D printed Nintendo Switch Game Carousel
0:14
Bambu Lab
Рет қаралды 4,5 МЛН