Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ The `while` loop performs the following steps for each task in the collection:
1. Awaits a call to `WhenAny` to identify the first task in the collection that has finished its download.

```csharp
Task<int> firstFinishedTask = await Task.WhenAny(downloadTasks);
Task<int> finishedTask = await Task.WhenAny(downloadTasks);
```

1. Removes that task from the collection.

```csharp
downloadTasks.Remove(firstFinishedTask);
downloadTasks.Remove(finishedTask);
```

1. Awaits `finishedTask`, which is returned by a call to `ProcessUrlAsync`. The `finishedTask` variable is a <xref:System.Threading.Tasks.Task%601> where `TResult` is an integer. The task is already complete, but you await it to retrieve the length of the downloaded website, as the following example shows. If the task is faulted, `await` will throw the first child exception stored in the `AggregateException`, unlike reading the <xref:System.Threading.Tasks.Task%601.Result?displayProperty=nameWithType> property, which would throw the `AggregateException`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ Make the following changes in `AccessTheWebAsync`. Asterisks mark the changes in
Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()
```

4. Call `WhenAny` on the collection of tasks. `WhenAny` returns a `Task(Of Task(Of Integer))` or `Task<Task<int>>`. That is, `WhenAny` returns a task that evaluates to a single `Task(Of Integer)` or `Task<int>` when it’s awaited. That single task is the first task in the collection to finish. The task that finished first is assigned to `firstFinishedTask`. The type of `firstFinishedTask` is <xref:System.Threading.Tasks.Task%601> where `TResult` is an integer because that's the return type of `ProcessURLAsync`.
4. Call `WhenAny` on the collection of tasks. `WhenAny` returns a `Task(Of Task(Of Integer))` or `Task<Task<int>>`. That is, `WhenAny` returns a task that evaluates to a single `Task(Of Integer)` or `Task<int>` when it’s awaited. That single task is the first task in the collection to finish. The task that finished first is assigned to `finishedTask`. The type of `finishedTask` is <xref:System.Threading.Tasks.Task%601> where `TResult` is an integer because that's the return type of `ProcessURLAsync`.

```vb
' ***Call WhenAny and then await the result. The task that finishes
' first is assigned to firstFinishedTask.
Dim firstFinishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)
' first is assigned to finishedTask.
Dim finishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)
```

5. In this example, you’re interested only in the task that finishes first. Therefore, use <xref:System.Threading.CancellationTokenSource.Cancel%2A?displayProperty=nameWithType> to cancel the remaining tasks.
Expand All @@ -90,10 +90,10 @@ Make the following changes in `AccessTheWebAsync`. Asterisks mark the changes in
cts.Cancel()
```

6. Finally, await `firstFinishedTask` to retrieve the length of the downloaded content.
6. Finally, await `finishedTask` to retrieve the length of the downloaded content.

```vb
Dim length = Await firstFinishedTask
Dim length = Await finishedTask
resultsTextBox.Text &= vbCrLf & $"Length of the downloaded website: {length}" & vbCrLf
```

Expand Down Expand Up @@ -180,16 +180,16 @@ Class MainWindow
Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()

' ***Call WhenAny and then await the result. The task that finishes
' first is assigned to firstFinishedTask.
Dim firstFinishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)
' first is assigned to finishedTask.
Dim finishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)

' ***Cancel the rest of the downloads. You just want the first one.
cts.Cancel()

' ***Await the first completed task and display the results
' Run the program several times to demonstrate that different
' websites can finish first.
Dim length = Await firstFinishedTask
Dim length = Await finishedTask
resultsTextBox.Text &= vbCrLf & $"Length of the downloaded website: {length}" & vbCrLf
End Function

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
1. Awaits a call to `WhenAny` to identify the first task in the collection to finish its download.

```vb
Dim firstFinishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)
Dim finishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)
```

2. Removes that task from the collection.

```vb
downloadTasks.Remove(firstFinishedTask)
downloadTasks.Remove(finishedTask)
```

3. Awaits `firstFinishedTask`, which is returned by a call to `ProcessURLAsync`. The `firstFinishedTask` variable is a <xref:System.Threading.Tasks.Task%601> where `TReturn` is an integer. The task is already complete, but you await it to retrieve the length of the downloaded website, as the following example shows.
3. Awaits `finishedTask`, which is returned by a call to `ProcessURLAsync`. The `finishedTask` variable is a <xref:System.Threading.Tasks.Task%601> where `TReturn` is an integer. The task is already complete, but you await it to retrieve the length of the downloaded website, as the following example shows.

```vb
Dim length = Await firstFinishedTask
Dim length = Await finishedTask
resultsTextBox.Text &= String.Format(vbCrLf & "Length of the downloaded website: {0}" & vbCrLf, length)
```

Expand Down Expand Up @@ -148,14 +148,14 @@ Class MainWindow
' ***Add a loop to process the tasks one at a time until none remain.
While downloadTasks.Count > 0
' ***Identify the first task that completes.
Dim firstFinishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)
Dim finishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)

' ***Remove the selected task from the list so that you don't
' process it more than once.
downloadTasks.Remove(firstFinishedTask)
downloadTasks.Remove(finishedTask)

' ***Await the first completed task and display the results.
Dim length = Await firstFinishedTask
Dim length = Await finishedTask
resultsTextBox.Text &= String.Format(vbCrLf & "Length of the downloaded website: {0}" & vbCrLf, length)
End While

Expand Down