Reputation: 9926
I try to understand why is better using the 'Async' method than using simple old synchronous way.
There is small issue that I don't understand.
On the synchronous way:
FileStream.Read(...)
. On the A-synchronous way:
TheadAsync01
') that calls method FileStream.ReadAsync(...)
. Now, When the IRP will signal that this IO request is finish what happened?
(The thread TheadAsync01
is now doing something else and can't continue the work with what the 'FileStream.ReadAsync
' return now.)
Is other thread will continue the continue the next action with the return value of the ReadAsync
?
What I don't understand here?
Upvotes: 1
Views: 323
Reputation: 22389
The reason it bothers you is this mistaken assumption:
The thread TheadAsync01 is now doing something else and can't continue the work with what the 'FileStream.ReadAsync' return now.
In a typical application I/O is by far the most time-consuming task.
When TPL is used correctly, threads are not blocked by time-consuming operations. Instead, everything time-consuming (in other words, any I/O) is delegated via await
. So when your IRP signals, the thread will either be free from work, or will be free very soon.
If there's some heavy calculation (something time-consuming which is not I/O), you need to plan accordingly, for example run it on a dedicated thread.
Upvotes: 5
Reputation: 957
The function ReadAsync immediately returns a value, namely a Task object. Somewhere you should do something with the return value. The canonical way is to use await:
await FileStream.ReadAsync(...)
This will ensure that the calling site will not continue with operation until ReadAsync has completed its job. If you want to do something in the meantime you could await the task object later or you can manually deal with the task object.
If you just call ReadAsync, ignoring the returned task object, doing nothing with it, then your reading is mostly an expensive no-op.
Upvotes: 2
Reputation: 2522
When a ***Async method returns a Task or Task you use this to track the running of the asynchronous operation. You can make the call behave synchronously with respect to the calling code by calling .Wait() on the task. Alternatively, as of .Net 4.5 you can await the task.
e.g:
private async void DoFileRead(...)
{
var result = await fileStream.ReadAsync(...);
// Do follow on tasks
}
In this scenario any follow on code would be wrapped in a continuation by the compiler and executed when the async call completed. One requirement of using the async keyword is to mark the calling method with the async keyword (see the example above).
Upvotes: 1