C
C#12mo ago
Claudiu HBann

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
Jimmacle
Jimmacle12mo ago
$nothread
MODiX
MODiX12mo ago
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
Jimmacle
Jimmacle12mo ago
basically, the UI thread can continue to do other work while waiting for the task to finish
Claudiu HBann
Claudiu HBannOP12mo ago
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
Jimmacle
Jimmacle12mo ago
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.
Claudiu HBann
Claudiu HBannOP12mo ago
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 little
Jimmacle
Jimmacle12mo ago
the first one is specific to IO bound work, where the CPU doesn't actually have any work to do to complete the operation
Claudiu HBann
Claudiu HBannOP12mo ago
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?
Jimmacle
Jimmacle12mo ago
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
Claudiu HBann
Claudiu HBannOP12mo ago
bro... wtf
jcotton42
jcotton4212mo ago
what about it?
Claudiu HBann
Claudiu HBannOP12mo ago
the blog post is named "There is no thread" and he says that the async method will contie on a THREAD pool
jcotton42
jcotton4212mo ago
@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
// blah blah
await FooAsync(); // thread becomes freed up here until FooAsync is done
// code from here on is the continuation
// blah blah
await FooAsync(); // thread becomes freed up here until FooAsync is done
// code from here on is the continuation
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
Want results from more Discord servers?
Add your server