【问题标题】:async task deadlock when call ExecuteAsync() function调用 ExecuteAsync() 函数时异步任务死锁
【发布时间】:2019-01-13 03:14:04
【问题描述】:

当我尝试从任务async function my function 返回任务结果时遇到问题:

public async Task<IEnumerable<string>> Run()
        {
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "API Key",


  ApplicationName = this.GetType().ToString()
        });

        var searchListRequest = youtubeService.Search.List("snippet");
        searchListRequest.Q = "anwar jibawi"; // Replace with your search term.
        searchListRequest.MaxResults = 50;

        // Call the search.list method to retrieve results matching the specified query term.
        var searchListResponse = await searchListRequest.ExecuteAsync();

        List<string> videos = new List<string>();
        List<string> channels = new List<string>();
        List<string> playlists = new List<string>();

        // Add each result to the appropriate list, and then display the lists of
        // matching videos, channels, and playlists.
        foreach (var searchResult in searchListResponse.Items)
        {
            switch (searchResult.Id.Kind)
            {
                case "youtube#video":
                    string thumbnail = searchResult.Snippet.Thumbnails.Default__.Url;
                    videos.Add(String.Format("{0} ({1}) {2}", searchResult.Snippet.Title, searchResult.Id.VideoId, thumbnail));
                    break;

                case "youtube#channel":
                    channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
                    break;

                case "youtube#playlist":
                    playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
                    break;
            }
        }

        return videos;
        //Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
        //Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
        //Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
    }

我在这里调用异步函数:

public ActionResult Index()
    {

        Task<IEnumerable<string>> task = new Search().Run();
        task.Wait();//if remove this line it will work fine but without any result
        var x = task.Result;//if remove this line it will work fine but without any result
        return View();
    }

为什么当我打电话给task.Wait()task.Reslut 时它会挂起

【问题讨论】:

  • 为什么不把方法改成Task&lt;ActionResult&gt;await这个任务呢?
  • 如果我更改了方法,如何返回我的 IEnumerable 视频?有吗?

标签: c# api model-view-controller async-await youtube-api


【解决方案1】:

假设这是一个 ASP.NET 应用程序,you shouldn't use .Result(或.Wait())因为它会导致死锁(正如你所发现的)

相反,将您的 Index 方法更改为此

public async Task<ActionResult> Index()
{

    var x = await new Search().Run();
    return View();
}

【讨论】:

  • 非常感谢,但请编辑您的答案,为新实例添加缺少的“()”等待新的 Search().Run(),再次感谢
  • await searchListRequest.ExecuteAsync() 上还应该有一个 .ConfigureAwait(false) 以防止这种情况发生,因为它不需要上下文中的上下文并且不知道调用者是否会同步做一些愚蠢的事情等等。
  • 是的,你的权利@GeorgeHelyar 我为我的代码添加了谢谢。
猜你喜欢
  • 2014-12-09
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
相关资源
最近更新 更多