An async/await example that causes a deadlock

后端 未结 5 1920
暗喜
暗喜 2020-11-21 13:32

I came across some best practices for asynchronous programming using c#\'s async/await keywords (I\'m new to c# 5.0).

One of the advices gi

5条回答
  •  不思量自难忘°
    2020-11-21 14:06

    Another main point is that you should not block on Tasks, and use async all the way down to prevent deadlocks. Then it will be all asynchronous not synchronous blocking.

    public async Task ActionAsync()
    {
    
        var data = await GetDataAsync();
    
        return View(data);
    }
    
    private async Task GetDataAsync()
    {
        // a very simple async method
        var result = await MyWebService.GetDataAsync();
        return result.ToString();
    }
    

提交回复
热议问题