我有以下代码:
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.
更多推荐
异步方法跳过等待
发布评论