异步方法跳过等待

编程入门 行业动态 更新时间:2024-10-24 06:30:08
本文介绍了异步方法跳过等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码:

public async Task<string> GetData(Uri source) { if (client.IsBusy == true) client.CancelAsync (); Task<string> tskResult = client.DownloadStringTaskAsync (source); string strResult = await tskResult; return strResult; }

当我从 Task< string> ... 开始逐步执​​行此方法时,调试器会跳过 return strResult; 并且strResult的值为null.

When I step through this method starting with Task<string>... the debugger jumps over return strResult; And the value of strResult is null.

为什么会这样?谢谢.

PS:我正在这样调用此方法:

PS: I am calling this method like this:

StringBuilder strBuild = new StringBuilder();

StringBuilder strBuild = new StringBuilder();

foreach (var image in imageApiUrlLst) { string imageModelPull = await callMgr.GetData(new Uri(image)); ///WHY GETS STUCK! strBuild.AppendLine(imageModelPull); }

推荐答案

如果等待的内容尚未到达,则 async 方法将在到达 await 语句后立即返回.没完.完成后,该方法将在该 await 语句之后继续执行.尝试在return语句上放置一个断点,它应该被击中两次.

An async method returns as soon as an await statement is reached, if the thing awaited hasn't finished. Once it completes, the method continues execution after that await statement. Try putting a break point on the return statement and it should get hit twice.

更多推荐

异步方法跳过等待

本文发布于:2023-11-10 02:15:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳过   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!