常规的迭代器块(即"yield“)是否与"async”和"await“不兼容?
这给了我一个很好的想法,我想要做什么:
async Task<IEnumerable<Foo>> Method(String [] Strs)
{
// I want to compose the single result to the final result, so I use the SelectMany
var finalResult = UrlStrings.SelectMany(link => //i have an Urlstring Collection
await UrlString.DownLoadHtmlAsync() //download single result; DownLoadHtmlAsync method will Download the url's html code
);
return finalResult;
}
然而,我得到一个编译器错误,引用"unable to load message string from resources“。
下面是另一个尝试:
async Task<IEnumerable<Foo>> Method(String [] Strs)
{
foreach(var str in strs)
{
yield return await DoSomethingAsync( str)
}
}
但是,编译器再次返回一个错误:"unable to load message string from resources“。
以下是我的项目中的实际编程代码
当我有一个列表任务时,这是非常有用的,这个任务可以从一个网址下载超文本标记语言,我使用了语法“very return await task",结果是我想要IEnumerable<Foo>
。我不想写这样的代码:
async Task<IEnumerable<String>> DownLoadAllURL(String [] Strs)
{
List<Foo> htmls= new ...
foreach(var str in strs)
{
var html= await DownLoadHtmlAsync( str)
htmls.Add(item)
}
return htmls;
}
但看起来我不得不这么做。
谢谢你的帮助。
https://stackoverflow.com/questions/5061761
复制相似问题