C# Tasks question
hello, idk if this is the right place to ask but i have a question about how the tasks in c# work, like, let's say we have an application with an GUI and on an event on a button click I await a task that it will take about 2-3 seconds, but the GUI doesnt freeze and it works just fine, the question is how, because I am wating for that task to finish on the UI thread and it should freeze.
As an example I have some experience with C++/WinRT WinUI3 and for something like this to work you need to switch the context with
winrt::resume_background()
and now you are on a thread from a thread pool, you do your job and next you switch back on the UI thread with winrt::resume_foreground(context)
, the context must be the one captured before resuming to the background or use the UI dispatcher to run something on the UI thread
There is a list of tasks and my task goes there and somewhere in time it runs on the UI thread too? the task contains multiple small tasks and are each of them go in the "list" one by one?13 Replies
$nothread
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
basically, the UI thread can continue to do other work while waiting for the task to finish
I can see that but the question is how is it working, like what is c# doing behind
i am going to read the post provided later
Async and Await
Most people have already heard about the new “async” and “await” functionality coming in Visual Studio 11. This is Yet Another Introductory Post.
so basically your first blog says that all the operations in the end are made by the hardware (read/write to the disk or a socket) and let's say you have a async method
A
with 2 awaits and the first is a socket read and the second is a disk write and the first operation starts and the method suspends and the control is returned to the caller and after a while when the operation is done there is an interrupt and the current running thread is suspended and the method is running right now and the first operation from the async method s completed and the next is taken and repeat?
about this one i am going to read it again because, maybe i read it wrong but, it contradicts the first blog a littlethe first one is specific to IO bound work, where the CPU doesn't actually have any work to do to complete the operation
so the logic i said it's fine? i understood correctly? only for IO operations?
when there is only the CPU involved everything is sync?
there are additional resources at the bottom of the second article that explain it better than i can
it's not necessarily sync
usually isn't
bro... wtf
what about it?
the blog post is named "There is no thread" and he says that the async method will contie on a THREAD pool
@Claudiu HBann "no thread" is about how there's no thread being tied up to wait for IO-bound work
what Cleary is talking about in that screenshot is where the continuation, the code after the
await
is running
where the continuation runs is controlled by the active synchronization context
if there is no sync context, eg in console apps or after a ConfigureAwait(false), the continuation is scheduled to the thread pool
common uses of sync contexts are in things like GUI apps, to ensure the continuation runs on the UI thread so things like controls can be interacted with