❔ 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
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.another thread?
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
what's a state machine?
Out of scope for this question :p
how do i use it efficiently?
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
yeah definitely
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
yeah thats where im at
the breakfast example is a classic
you dont need to wait for your eggs to be done before you start making toast
but you need to know when it's done right
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?)
this would start both and then wait for both to finish
this is somewhat common, so there are helper methods for this
so await blocks until the task is done?
doesnt block
it halts the state machine
but the thread remains unblocked
so how do other things use the thread?
like an app ui
well the thread isnt blocked, so it just... uses it?
when async is involved, the runtime does some thread management
so is an app ui async or not?
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
read all three parts
ok
how about multithreading?
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
cpu bound?
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
oh ok
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.