I read that: when the logic thread reaches the wait token, the calling thread pauses until the call ends.
Where did you read this bullshit? There is some context there that you are not quoting, or you should stop reading any text that contains this. The expected point is the opposite . The expected point is to keep the current thread useful while the asynchronous task is in flight.
UPDATE: I downloaded the book you referenced. Absolutely everything in this section is incorrect. Drop this book and buy the best book.
I don’t understand that when I click the button, label1 will have the text Running, and the label will have the same text only after 10 seconds, but in these 10 seconds I was able to enter text into my text box, so it seems that the main thread is started. ..
It is right. Here's what happens:
label1.Text = Thread.CurrentThread.ThreadState.ToString();
The text is set.
button1.Text = await DoWork();
A lot of things are happening here. What happens first? DoWork . What is he doing?
return Task.Run(() => { Thread.Sleep(10000);
It grabs a thread from the thread pool, puts the thread to sleep for ten seconds, and returns a task representing the "work" performed by that thread.
Now we are back here:
button1.Text = await DoWork();
We have a task. Waiting first checks the task to make sure it is already completed. Is not. He then signs the remainder of this method as a continuation of the task. He then returns to the caller.
Hey, what's his call? How did we get here?
Some code called this event handler; it was an event loop processing windows messages. He saw that a button had been pressed and sent to the click handler that had just returned.
Now what is going on? The event loop continues to work. As you noticed, your user interface works well. In the end, this branch goes out for ten seconds and the continuation of the task is activated. What does it do?
It sends a message to the Windows queue, saying "you need to run the rest of this event handler, I have the result you were looking for."
The main flow event loop ends up in this message. Therefore, the event handler selects where it stopped:
button1.Text = await DoWork();
Waiting now extracts the result from the task, saves it in the button text and returns to the event loop.