Trying to understand why .NET implemented async-wait , how they did it.
When changing a simple section of code to use async waiting, it seems that the least required effort is to mark both the calling and the called methods with asynchronous expectations:
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? those. it would be useful to indicate with one keyword that the expression should be immediately evaluated asynchronously in the workflow - while the calling GUI thread is freed up for other tasks, but will be reconnected to execute the remaining code once the workflow is executed.
Is there an easier or better way to assign a workflow to process the Work method and then automatically (without having to resort to Invoke (...)) so that the same GUI call flow will process 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 says that the compiler will execute the code synchronously if the target does not contain a wait statement - but then what point? - Of course, pending async keywords are only for asynchronous use. Then why does it become so complicated instead of using a single directive? )
source
share