简体   繁体   English

如何使用异步/等待来延迟任务而不阻塞UI

[英]How to use async/await to delay task without blocking UI

I know I am asking an exist question. 我知道我在问一个存在的问题。 I read many articles about this but I still confused. 我读了许多有关此的文章,但我仍然感到困惑。 Maybe my English skill is not good enough. 也许我的英语水平不够好。

Here is my code at first: 首先是我的代码:

void dovoid1(){
//dosomething
}
void dovoid2(){
//dosomething
}
void dovoid3(){
//dosomething
}

and

void action()
{
            dovoid1();
            Thread.Sleep(1000);
            dovoid2();
            Thread.Sleep(1000);
            dovoid3();
            Thread.Sleep(1000);
            action();
}

As you see, the void action() will do some task, and have a sleep between them. 如您所见,void action()将完成一些任务,并使它们之间处于睡眠状态。 Afer that, it repeat itself. 之后,它会自我重复。 Now I want to avoid Thread.Sleep() because it blocks the UI. 现在,我要避免Thread.Sleep(),因为它会阻塞UI。 So I try to use async/await. 所以我尝试使用异步/等待。

private async void action()
        {

            dovoid1();
            Task.Delay(1000);
            dovoid2();
            Task.Delay(1000);
            dovoid3();
            Task.Delay(1000);
            action();
        }

But it give me errors. 但这给了我错误。 I don't know where and When I should use async or await. 我不知道何时何地应该使用异步或等待。 Thank you! 谢谢!

You need to await the delays to create the desired time gap between calls. 您需要等待延迟以在两次呼叫之间创建所需的时间间隔。 Await in this context yields control until Task.Delay completes. 在此上下文中等待产生控制权,直到Task.Delay完成。

Also, if action() is not an event handler then it should probably be async Task instead of async void (see why is void async bad? ). 另外,如果action()不是事件处理程序,则它可能应该是async Task而不是async void (请参见为什么void异步不好? )。

private async Task action()
{
    dovoid1();
    await Task.Delay(1000);
    dovoid2();
    await Task.Delay(1000);
    dovoid3();
    await Task.Delay(1000);
    action();
}

you can use this approach in your parent function for non-blocking your UI 您可以在父函数中使用此方法来不阻塞用户界面

await Task.Run(() => {
    // Do lot of work here
});

in your case 在你的情况下

await Task.Run(() => action());

if your action method is async 如果您的操作方法是异步的

async Task action(){}

then 然后

await Task.Run(async () => await action());

you can simply convert your action method to async by putting await keyword on every Task. 您只需在每个Task上放置await关键字,即可将您的操作方法简单地转换为异步操作。 Since await keyword can only be used with async method, You need to convert your method to async. 由于await关键字只能与异步方法一起使用,因此您需要将方法转换为异步。

async Task action()
{
            dovoid1();
            await Task.Delay(1000);
            dovoid2();
            await Task.Delay(1000);
            dovoid3();
            await Task.Delay(1000);
            action();
}

please keep in mind that if your dovoid1, dovoid2, dovoid3 includes Task then they are also needed to be converted to async method and needed to be awaited. 请记住,如果您的dovoid1,dovoid2,dovoid3包含Task,则还需要将它们转换为异步方法并等待它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM