C
C#17mo ago
circles.png

❔ 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
Pobiega17mo 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.png17mo ago
another thread?
Pobiega
Pobiega17mo 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.png17mo ago
what's a state machine?
Pobiega
Pobiega17mo ago
Out of scope for this question :p
circles.png
circles.png17mo ago
how do i use it efficiently?
Pobiega
Pobiega17mo 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.png17mo ago
yeah definitely
Pobiega
Pobiega17mo 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.png17mo ago
yeah thats where im at
Pobiega
Pobiega17mo 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.png17mo ago
but you need to know when it's done right
Pobiega
Pobiega17mo 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.png17mo ago
so await blocks until the task is done?
Pobiega
Pobiega17mo ago
doesnt block it halts the state machine but the thread remains unblocked
circles.png
circles.png17mo ago
so how do other things use the thread? like an app ui
Pobiega
Pobiega17mo ago
well the thread isnt blocked, so it just... uses it? when async is involved, the runtime does some thread management
circles.png
circles.png17mo ago
so is an app ui async or not?
Pobiega
Pobiega17mo 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
Pobiega17mo ago
read all three parts
circles.png
circles.png17mo ago
ok how about multithreading?
Pobiega
Pobiega17mo 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.png17mo ago
cpu bound?
Pobiega
Pobiega17mo 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.png17mo ago
oh ok
Accord
Accord17mo 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
More Posts