I've read that: When the flow of logic reaches the await token, the calling thread is suspended until the call completes.
Where did you read that nonsense? Either there is some context there that you're not quoting, or you should stop reading whatever text it is that contained this. The point of await is to do the opposite of that. The point of await is to keep the current thread doing useful work while the asynchronous task is in flight.
UPDATE: I downloaded the book you referenced. Absolutely everything in that section is wrong. Throw this book away and buy a better book.
What I don't understand is that when I click the button, the 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 the text in my textbox, so it seems that the main thread is running...
That's correct. Here's what happens:
label1.Text = Thread.CurrentThread.ThreadState.ToString();
The text is set.
button1.Text = await DoWork();
A bunch of stuff happens here. What happens first? DoWork
is called. What does it do?
return Task.Run(() => { Thread.Sleep(10000);
It grabs a thread out of the thread pool, puts that thread to sleep for ten seconds, and returns a task representing the "work" being done by that thread.
Now we are back here:
button1.Text = await DoWork();
We have a task in hand. Await first checks the task to see if it is already complete. It is not. Next it signs up the remainder of this method as the continuation of the task. Then it returns to its caller.
Hey, what is its caller? How did we get here anyways?
Some code called this event handler; it was the event loop that is processing Windows messages. It saw a button was clicked and dispatched to the click handler, which has just returned.
Now what happens? The event loop keeps running. Your UI keeps on running nicely, as you noticed. Eventually that thread ticks off ten seconds and the task's continuation is activated. What does that do?
That posts a message into the Windows queue saying "you need to run the rest of that event handler now; I have the result you were looking for."
The main thread event loop eventually gets to that message. So the event handler picks up where it left off:
button1.Text = await DoWork();
The await now extracts the result from the task, stores it in the button text, and returns back to the event loop.
async
/await
in your example and are running on a seperate thread (Task.Run
runs from the thread pool). – TamathatamaulipasDoWork
and the button event, moreover you haven't exposed theGetText
method. Additionally, "the same text only after 10 seconds" is a bit murky. "i was able to enter text in my textbox" you are never limited to this option. Please edit and explain your issue into more detail. – LarryThread.Suspend
API. This is certainly not the case withawait
, unless some special efforts are taken. Perhaps, the author implied here something like logical execution flow rather than thread. – PsychognosisC# in Depth
,Essential C#
,C# in a Nutshell
. I edited all three; they are accurate. – TrimetallicRun
will obtain the string. Whoever wrote this thing is just making stuff up. None of this is at all correct. It's like the author has thought "how would I implement this feature?" and described that, rather than describing the actual feature as we implemented it. – Trimetallicasync
intro blog post that may help you, where I try to introduce how to think about async/await without overwhelming you. – Policewoman