Trying to understanding why .NET implemented async-await they way they did.
When changing a simple section of code to use async-await, it seems like the least required effort is to mark both the calling and the called methods with async-await directives:
private async void OnGuiClick(object sender, EventArgs args)
{
textBox1.Text = await Work();
}
private async Task<string> Work()
{
await Task.Delay(2000);
return "Result";
}
Why does .NET insist on both? i.e. It would have been nice to specify with a single keyword, that an expression must immediately be evaluated asynchronously on a worker thread - while the calling GUI thread is freed to perform other tasks, but will be hooked up again to execute the remaining code once the worker thread is done.
Is there a simpler or better way of assigning a worker thread to process the Work method, and then automatically (without needing to resort to Invoke(...)) ensure the same calling GUI thread processes the result? Why not something like this:
private void OnGuiClick(object sender, EventArgs args)
{
textBox1.Text = <'some async directive'> Work();
}
private string Work()
{
Thread.Sleep(2000);
return "Result";
}
(The MSDN documentation states that the compiler will execute code synchronously if the target does not contain an await statement - but then what's the point? - Surely the await async keywords are only intended for asynchronous usage. Then why make it so complicated, instead of using a single directive?)