I used How to: Extend the async Walkthrough by Using Task.WhenAll (C#) to develop a program to independently submit multiple database queries asynchronously. The program works ok, but there is a nuance I don't understand and for which I can't find an explanation:
In the example, the signature for the method SumPageSizesAsync
is:
private async Task SumPageSizesAsync()
The body of the method SumPageSizesAsync
does not explicitly return the whenAllTask
object (i.e., the WhenAll
roll-up Task
). Why doesn't the method SumPageSizesAsync
explicitly return the whenAllTask
task? How does the line of code in StartButton_Click
that calls SumPageSizesAsync
"know" which task is being returned?
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
.
Task sumTask = SumPageSizesAsync();
.
}
private async Task SumPageSizesAsync()
{
HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 };
List<string> urlList = SetUpURLList();
IEnumerable<Task<int>> downloadTasksQuery = from url in urlList select ProcessURLAsync(url);
Task<int>[] downloadTasks = downloadTasksQuery.ToArray();
Task<int[]> whenAllTask = Task.WhenAll(downloadTasks);
int[] lengths = await whenAllTask;
int total = lengths.Sum();
// Display the total count for all of the web addresses.
resultsTextBox.Text += string.Format("\r\n\r\nTotal bytes returned: {0}\r\n", total);
// where's the return statement?
}