相关疑难解决方法(0)

如何以及何时使用'async'和'await'

从我的理解主要事情之一asyncawait要做的就是让代码易于读写-但使用它们等于产卵后台线程来执行持续时间长的逻辑?

我正在尝试最基本的例子.我在内联添加了一些评论.你能为我澄清一下吗?

// 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 …
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous async-await

989
推荐指数
19
解决办法
78万
查看次数

标签 统计

.net ×1

async-await ×1

asynchronous ×1

c# ×1