C# 调用ExecuteAsync()函数时异步任务死锁
尝试从taskC# 调用ExecuteAsync()函数时异步任务死锁,c#,api,model-view-controller,async-await,youtube-api,C#,Api,Model View Controller,Async Await,Youtube Api,尝试从taskasyncfunction my function返回任务结果时出现问题: public async Task<IEnumerable<string>> Run() { var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "API Key",
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)));
}
公共异步任务运行()
{
var youtubeService=new youtubeService(new BaseClientService.Initializer()
{
ApiKey=“API密钥”,
ApplicationName=this.GetType().ToString()
});
var searchListRequest=youtubeService.Search.List(“代码段”);
searchListRequest.Q=“anwar jibawi”//替换为您的搜索词。
searchListRequest.MaxResults=50;
//调用search.list方法检索与指定查询词匹配的结果。
var searchListResponse=等待searchListRequest.ExecuteAsync();
列表视频=新建列表();
列表通道=新列表();
列表播放列表=新建列表();
//将每个结果添加到相应的列表中,然后显示结果列表
//匹配视频、频道和播放列表。
foreach(searchListResponse.Items中的var searchResult)
{
开关(searchResult.Id.Kind)
{
案例“youtube视频”:
string thumbnail=searchResult.Snippet.Thumbnails.Default\u\u.Url;
添加(String.Format(“{0}({1}){2}”,searchResult.Snippet.Title,searchResult.Id.VideoId,缩略图));
打破
案例“youtube频道”:
Add(String.Format(“{0}({1})”,searchResult.Snippet.Title,searchResult.Id.ChannelId));
打破
案例“youtube播放列表”:
Add(String.Format(“{0}({1})”,searchResult.Snippet.Title,searchResult.Id.playlaid));
打破
}
}
返回视频;
//WriteLine(String.Format(“Videos:\n{0}\n”,String.Join(“\n”,Videos));
//WriteLine(String.Format(“Channels:\n{0}\n”,String.Join(“\n”,Channels));
//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();
}
public ActionResult Index()
{
任务=新建搜索().Run();
task.Wait();//如果删除此行,它将正常工作,但不会产生任何结果
var x=task.Result;//如果删除此行,它将正常工作,但不会产生任何结果
返回视图();
}
为什么在调用
task.Wait()
或task.Reslut
时挂起,假设这是一个ASP.NET应用程序,(或.Wait()
),因为它会导致死锁(如您所发现的)
相反,将索引方法更改为
public async Task<ActionResult> Index()
{
var x = await new Search().Run();
return View();
}
公共异步任务索引()
{
var x=等待新搜索().Run();
返回视图();
}
为什么不将方法更改为任务
和等待
任务?如果更改了方法,如何返回IEnumerable视频?还有吗?这是真的,非常感谢,但是编辑您的答案,为新实例添加缺少的“()”,等待新的搜索().Run(),再次感谢等待搜索列表请求。ExecuteAsync()
上还应该有一个.ConfigureAwait(false)
,以防止出现这种情况,因为它不需要继续中的上下文,也不知道调用方是否会做一些愚蠢的事情,比如同步等待。是的,你的权利@GeorgeHelyar,我为我的代码添加了感谢。