尝试理解执行ToArray()时任务启动的原因。 以及为什么我在DoSomething方法中放置的断点永远不会被击中。
private async void MyMethod()
{
await NewMethod();
string[] array = { "http://google.com", "http://microsoft.com"};
IEnumerable<Task<string>> query = from url in array select DoSomething(url);
Task<string>[] tasks = query.ToArray();
string[] contents = await Task.WhenAll(tasks);
}
private async Task<string> DoSomething(string url)
{
HttpClient hc = new HttpClient();
string content = await hc.GetStringAsync(url);
return content;
}
如果ToArray()实际启动任务,执行应该停在DoSomething方法的断点处,是否正确?
谢谢