❔ Can someone explain asynchronous code?

I've read the overview with the breakfast analogy on the Microsoft docs for .NET, and still don't get - how to use it (and efficiently) - how .NET is able to do many things on one thread at the same time There is one way I think I understand. - callbacks - when a task is done, call a function to deal with the result - probably not very good - JS thing
27 Replies
Pobiega
Pobiega2y ago
How to use it: async Task<T> as your return type, and use await in the body when calling other async methods. When to use it: When calling async operations, mostly IO related work (reading files, querying database, making http requests etc). How does it work: whenever you await, it creates a state machine that keeps track of where in your method you are. The async thread will jump around and check if its ready to move on or not.
circles.png
circles.pngOP2y ago
another thread?
Pobiega
Pobiega2y ago
The short answer is maybe. Depends on a lot of stuff. But in a very simple application, say a console app with an async main thread and just doing a web request and waiting for it to finish, nope, same thread
circles.png
circles.pngOP2y ago
what's a state machine?
Pobiega
Pobiega2y ago
Out of scope for this question :p
circles.png
circles.pngOP2y ago
how do i use it efficiently?
Pobiega
Pobiega2y ago
I have made a 20 min video on the topic a while ago thats still relevant, feel free to check it out should you wish. https://www.youtube.com/watch?v=NQPOCgtnzR0
circles.png
circles.pngOP2y ago
yeah definitely
Pobiega
Pobiega2y ago
the short answer is "just use it". it will help with stuff like "my app freezes while I do a http request!" the next step up is trying to think about what must be awaited and when
circles.png
circles.pngOP2y ago
yeah thats where im at
Pobiega
Pobiega2y ago
the breakfast example is a classic you dont need to wait for your eggs to be done before you start making toast
circles.png
circles.pngOP2y ago
but you need to know when it's done right
Pobiega
Pobiega2y ago
yes and no depends. Do you want to do something immediately when the eggs are done, regardless of when the eggs finish? or do you just want to wait for both eggs AND toast to be done before you do something else (make eggy toast?)
var makingToast = MakeToast(); // MakeToast is `async Task`
var makingEggs = MakeEggs();

await makingToast;
await makingEggs;
var makingToast = MakeToast(); // MakeToast is `async Task`
var makingEggs = MakeEggs();

await makingToast;
await makingEggs;
this would start both and then wait for both to finish this is somewhat common, so there are helper methods for this
var makingToast = MakeToast();
var makingEggs = MakeEggs();

await Task.WhenAll(makingEggs, makingToast);
var makingToast = MakeToast();
var makingEggs = MakeEggs();

await Task.WhenAll(makingEggs, makingToast);
circles.png
circles.pngOP2y ago
so await blocks until the task is done?
Pobiega
Pobiega2y ago
doesnt block it halts the state machine but the thread remains unblocked
circles.png
circles.pngOP2y ago
so how do other things use the thread? like an app ui
Pobiega
Pobiega2y ago
well the thread isnt blocked, so it just... uses it? when async is involved, the runtime does some thread management
circles.png
circles.pngOP2y ago
so is an app ui async or not?
Pobiega
Pobiega2y ago
by default, no if you make a winforms/wpf app and make a button that makes a HTTP request in the click handler, your app will freeze while it waits for the answer If you want to know how async really works under the hood, here is a decent article to get started
Pobiega
Pobiega2y ago
read all three parts
circles.png
circles.pngOP2y ago
ok how about multithreading?
Pobiega
Pobiega2y ago
completely different topic or well, largely different topic. You can run a Task on a different thread intentionally by using Task.Run this is often done to "offload" heavy CPU work from a main thread if your async method is IO bound -> use a normal await if your async method is CPU bound -> use Task.Run
circles.png
circles.pngOP2y ago
cpu bound?
Pobiega
Pobiega2y ago
its not true at all times, but its a good baseline yeah, like, what kind of work is it doing? calculating a certain location in the mandlebrot set? thats CPU work fetching images from the internet? thats IO work
circles.png
circles.pngOP2y ago
oh ok
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server