Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// Example 1) async await is redundant when calling a method that doesn't block anyway
//
void Main()
{
var filename = Path.Combine(Path.GetTempPath(), "ExampleAsyncAwaitIsRedundant.txt");
using (var fstream= new FileStream(filename,FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096,FileOptions.Asynchronous))
{
fstream.Seek(0, SeekOrigin.Begin);
WriteToStreamAsyncAwaitAsync(fstream);
WriteToStreamJustPlainAsync(fstream);
Process.Start(filename);
Console.WriteLine($"Wrote to {filename} which has Async={fstream.IsAsync}");
}
}
async void WriteToStreamAsyncAwaitAsync(FileStream writeable)
{
var txt= Encoding.UTF8.GetBytes($"AsyncAwaitAsync {DateTime.Now.Ticks}\n");
await writeable.WriteAsync(txt,0,txt.Length);
}
void WriteToStreamJustPlainAsync(FileStream writeable)
{
var txt = Encoding.UTF8.GetBytes($"JustPlainAsync {DateTime.Now.Ticks}\n");
writeable.WriteAsync(txt, 0, txt.Length);
}
@chrisfcarroll

This comment has been minimized.

Copy link
Owner Author

chrisfcarroll commented Jan 24, 2017

The point of await is to simplify the coding of a callback. It doesn't add extra asynchronicity.
The async keyword on a method simplifies the wrapping of a return value in a Task. That does add asynchronicity – the Task gets scheduled – but awaiting it immediately throws away that asynchronicity by, well, waiting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.