根据我的理解,#0和#1所做的主要事情之一是使代码易于编写和阅读-但使用它们是否等于生成后台线程来执行长时间逻辑?
我现在正在尝试最基本的例子。我已经内联添加了一些注释。你能为我澄清一下吗?
// I don't understand why this method must be marked as `async`.private async void button1_Click(object sender, EventArgs e){Task<int> access = DoSomethingAsync();// task independent stuff here
// this line is reached after the 5 seconds sleep from// DoSomethingAsync() method. Shouldn't it be reached immediately?int a = 1;
// from my understanding the waiting should be done here.int x = await access;}
async Task<int> DoSomethingAsync(){// is this executed on a background thread?System.Threading.Thread.Sleep(5000);return 1;}