Excuse my ignorance, but I am having problems understanding the MSDN excerpt for reading file contents asynchronously
https://msdn.microsoft.com/en-us/library/jj155757.aspx
string text = await ReadTextAsync(filePath);
...
private async Task<string> ReadTextAsync(string filePath)
{
using (FileStream sourceStream = new FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.Read,
bufferSize: 4096, useAsync: true))
{
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[0x1000];
int numRead;
while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
string text = Encoding.Unicode.GetString(buffer, 0, numRead);
sb.Append(text);
}
return sb.ToString();
}
}
How is that reading asynchronously? It appears to wait until ReadTextAsync
has completed to return the contents. If there were a Thread.Sleep
inserted inside that method, then it would wait to complete, and no further code would run, after the call to the method.