So i've read a lot of articles lately by trying to understand how the async methods works in the .NET world.
I've tried really hard to understand how an async call works, from actually calling HttpClient.GetAsync("https://google.com")
, all the way down to how the OS initiates the call on the network card.
What bothers me is how the state machine knows when to call it's MoveNext()
method.
From the network card, we have a system interrupt (once we have the request response), which seizes the control over the CPU for a brief moment. The CPU creates a so called Deferred Procedure Call (DPC)
which the OS kernel keeps tracks of and executes these DPC's with high priority. After that the kernel initiates an APC (asynchronous procedure call)
which, in bigger words, notifies the thread which made the async call and marks the Task
as completed, and can resume it's execution.
My question is:
In a windows forms application, we have a UI Thread (which draws the window on the screen and responds to user events). Does this UI Thread have some sort of a Task queue
which keeps on interogating from time to time, and asks if one of the tasks in the Completed state ? If yes, then everything makes sense, and my original question is answered. Once the OS sets the tasks as completed, and the UI thread sees that, it will call the MoveNext()
method on it's State Machine.
But i heard recently that the UI Thread doesn't have this kind of Task queue
. If this is the case, then how the OS, and internally the app itself, knows how to trigger the MoveNext()
method ?
One assumption that i made, is that the OS injects the code of MoveNext()
in whatever thread is currently available. But it still weird.