如何等待无返回任务方法

时间:2018-02-21 10:19:14

标签: c# async-await

我正在了解asyncawait。我有一个异步的对话框消息方法,我正在等待另一种方法。

我的代码=>

private async Task CalculateProcess()
{
    dialogCoordinator = DialogCoordinator.Instance;
    // Show...
    ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEADER", "MESSAGE");
    controller.SetIndeterminate();

    // Do your work... 
    await testing();//I want to await testing method but i cant await testing because testing method no return task
    // Close...
    await controller.CloseAsync();
}
private void testing()
{
   for(int i=0;i<=1000000;i++)
    {

    }//Looping is example                
}

我想等待测试方法但是如果我想使用await我需要从testing返回任务。如果我没有任何其他返回任务,我可以等待测试方法吗?

1 个答案:

答案 0 :(得分:1)

如果你真的想要等待这种方法,那就是这样的方法:

private async Task CalculateProcess()
{
    dialogCoordinator = DialogCoordinator.Instance;
    // Show...
    ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEADER", "MESSAGE");
    controller.SetIndeterminate();

    // Do your work... 
    await testing();
    // Close...
    await controller.CloseAsync();
}

private Task testing()
{
   for(int i=0;i<=1000000;i++)
    {

    }//Looping is example                

    return Task.FromResult(0);
}
相关问题