通过多种方法调用async / await

时间:2014-06-20 14:51:21

标签: c# asynchronous

我有一个使用async / await的方法:

    private async void Start_Click(object sender, EventArgs e)
    {
        await M1Async();
    }

    static async Task M1Async()
    {
        await M2Async();
    }

    static async Task M2Async()
    {
        await M3Async();
    }

    static async Task M3Async()
    {
        await M4Async();
    }

    static async Task M4Async()
    {
        // start download using async scenario
        await StaryDownloadAsync();
    }

在这种情况下,什么是好的做法?我应该使用async / await来装饰所有方法,还是只使用执行实际异步方案的方法?

提前致谢。

1 个答案:

答案 0 :(得分:0)

取决于您的实际情况,但这样做很简单:

private async void Start_Click(object sender, EventArgs e)
{
    await M1Async();
}

private static Task M1Async()
{
    return M2Async();
}

private static Task M2Async()
{
    return M3Async();
}

private static Task M3Async()
{
    return M4Async();
}

private static Task M4Async()
{
    return Task.Delay(1000);
}