Are regular iterator blocks (i.e. "yield return") incompatible with "async" and "await"?
This gives a good idea of what I'm trying to do:
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;
}
However, I get a compiler error citing "unable to load message string from resources".
Here is another attempt:
async Task<IEnumerable<Foo>> Method(String [] Strs)
{
foreach(var str in strs)
{
yield return await DoSomethingAsync( str)
}
}
But again, the compiler returns an error: "unable to load message string from resources".
Here is the real programming code in my project
This is very useful when I have an List Task,that task can be download HTML from a URL
and I use the syntax "yield return await task", the result is I want IEnumerable<Foo>
. I don't want write this code:
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;
}
But it seems that I have to.
Thanks for any help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…