Reputation: 115
According to Microsoft Visual C# Step by Step 9th edition:
The await operator indicates that a method should be run by separate task, and the calling code is suspended until the method call completes. The thread used by the calling code is released and reused. This is important if thread is the user interface thread, as it enables the user interface to remain responsive.
Suppose I have a method that is IO bound and frees up the CPU:
private async Task DoLongRunningIO()
{
...
}
Does the above paragraph mean that running
await DoLongRunningIO();
message.Text = "Done";
in the UI thread will still keep the UI responsive since the UI thread is released? (as opposed to DoLongRunningIO().Wait()
, which would block the UI thread)
If the answer is yes, I assume it wouldn't be true if the long running task was CPU intensive instead, since the UI thread is still consumed in that case?
Upvotes: 7
Views: 3130
Reputation: 70701
Does the above paragraph mean that running
await DoLongRunningIO(); message.Text = "Done";
in the UI thread will still keep the UI responsive since the UI thread is released?
Yes.
If the answer is yes, I assume it wouldn't be true if the long running task was CPU intensive instead, since the UI thread is still consumed in that case?
No, your assumption is incorrect. A "thread" and the "CPU" are not the same thing. Even if your machine has only a single CPU, there can be one CPU-intensive thread running, and the UI thread running, and the OS will share the CPU between them. You may find a slightly degradation in responsiveness, but the UI thread will still be able to run.
If you have more than one CPU core, as is the case with pretty much all modern hardware, then as long as the two threads don't interact, the UI thread will be able to run unimpeded, even if there is also a CPU-intensive thread running.
Note that this all assumes that the DoLongRunningIO()
method was written correctly, i.e. does in fact reflect an operation that is handled asynchronously. For a CPU-intensive task, this would generally include a call to Task.Run()
to execute the operation, though there are of course other mechanisms that could be used instead.
If that method is written incorrectly, then all bets are off. You didn't provide any details about that method, so it's impossible to say whether that's the case in your scenario.
Aside:
For what it's worth, I take issue with the phrasing "The await operator indicates that a method should be run by separate task" that you quoted from the book. The await
operator says nothing about how a method or operation should be run/called/carried out/etc. The only thing that await
does is indicate a point in the current method where, given an awaitable object (typically a Task
), the method should return to the caller if the object is not in the completed state. It is up to whatever expression generates the awaitable object to handle how that task is created and whether it is done in a separate thread or otherwise in an asynchronous manner.
Upvotes: 4