Reputation: 4224
suppose sample snippet:
public async void writeDataOnFile (byte[] data, string filePath) {
using(var fs = new FileStream(filePath)) {
await File.WriteAsync(data,0,data.length);
}
}
I know that no background thread is consumed from thread pool,but an IO operation will be started and thread will be notified at the end.
Question is: HOW does async/await understand that WriteAsync is an IO-bound method rather than a CPU-bound method?
Any help would be appreciated.
Upvotes: 1
Views: 902
Reputation: 27009
Question is: HOW async/await understands that WriteAsync is an IO method rather than a CPU method?
The await
keyword does not care whether it is IO or CPU bound. All it cares about is that the "call" is awaitable. Please see here for more details.
See this example below where the extension method returns a TaskAwaiter
:
public static class ThisWorks
{
public static System.Runtime.CompilerServices.TaskAwaiter GetAwaiter(this Int32 millisecondsDue)
{
return TimeSpan.FromMilliseconds(millisecondsDue).GetAwaiter();
}
private static System.Runtime.CompilerServices.TaskAwaiter GetAwaiter(this TimeSpan timeSpan)
{
return Task.Delay(timeSpan).GetAwaiter();
}
}
Now to use that we can do this:
await 15;
There are better examples but at least this shows the point.
Upvotes: 3