當其中一項工作完成時,取消剩餘的非同步工作 (Visual Basic)

搭配使用 Task.WhenAny 方法與 CancellationToken,即可在其中一個工作完成時取消所有剩餘的工作。 WhenAny 方法會接受本身為一組工作的引數。 這個方法會啟動所有工作,並傳回單一工作。 集合中的任何工作完成時,單一工作即完成。

這個範例示範如何搭配使用取消權杖與 WhenAny 以完成這組工作中的第一項工作,並取消其餘工作。 每一項工作都會下載網站的內容。 此範例顯示要完成的第一個下載內容的長度,並取消其他下載。

注意

若要執行範例,您必須在電腦上安裝 Visual Studio 2012 或更新版本以及 .NET Framework 4.5 或更新版本。

下載範例

您可以從 Async Sample: Fine Tuning Your Application (非同步範例:微調應用程式) 下載完整 Windows Presentation Foundation (WPF) 專案,然後遵循下列步驟。

  1. 解壓縮您下載的檔案,然後啟動 Visual Studio。

  2. 在功能表列上,依序選擇 [檔案] 、[開啟舊檔] 及 [專案/方案]

  3. 在 [開啟專案] 對話方塊中,開啟保存已解壓縮之範例程式碼的資料夾,然後開啟 AsyncFineTuningVB 的方案 (.sln) 檔案。

  4. 在方案總管中,開啟 CancelAfterOneTask 專案的捷徑功能表,然後選擇 [設定為啟始專案]

  5. 選擇 F5 鍵以執行專案。

    選擇 CTRL+F5 鍵以執行專案,而不進行偵錯。

  6. 執行程式數次,確認先完成不同的下載。

如果您不想要下載專案,則可以檢閱本主題結尾的 MainWindow.xaml.vb 檔案。

建置範例

本主題中的範例會新增至取消一項非同步工作或工作清單中所開發的專案來取消工作清單。 雖然未明確地使用 [取消] 按鈕,但是此範例會使用相同的 UI。

若要自行逐步建置範例,請遵循<下載範例>一節中的指示,但選擇 [CancelAListOfTasks] 作為 [啟始專案]。 將本主題中的變更新增至該專案。

CancelAListOfTasks 專案的 MainWindow.xaml.vb 檔案中,將每個網站的處理步驟從 AccessTheWebAsync 中的迴圈移至下列非同步方法即可開始轉換。

' ***Bundle the processing steps for a website into one async method.
Async Function ProcessURLAsync(url As String, client As HttpClient, ct As CancellationToken) As Task(Of Integer)

    ' GetAsync returns a Task(Of HttpResponseMessage).
    Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)

    ' Retrieve the website contents from the HttpResponseMessage.
    Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync()

    Return urlContents.Length
End Function

AccessTheWebAsync 中,這個範例會使用查詢、ToArray 方法和 WhenAny 方法來建立並啟動工作陣列。 將 WhenAny 套用至陣列,會傳回評估為在工作陣列中完成之第一個工作的等候中單一工作。

AccessTheWebAsync 中進行下列變更。 星號會標記程式碼檔中的變更。

  1. 註解化或刪除迴圈。

  2. 建立查詢,而查詢在執行時會產生一組泛型工作。 每個 ProcessURLAsync 呼叫都會傳回 TResult 為整數的 Task<TResult>

    ' ***Create a query that, when executed, returns a collection of tasks.
    Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
        From url In urlList Select ProcessURLAsync(url, client, ct)
    
  3. 呼叫 ToArray 來執行查詢,並開始工作。 在下一個步驟中套用 WhenAny 方法會執行查詢並啟動工作,而不使用 ToArray,但其他方法可能為否。 最安全的做法是明確地強制執行查詢。

    ' ***Use ToArray to execute the query and start the download tasks.
    Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()
    
  4. 對這組工作呼叫 WhenAnyWhenAny 會傳回 Task(Of Task(Of Integer))Task<Task<int>>。 亦即,WhenAny 會傳回評估為單一 Task(Of Integer)Task<int> 的等候中工作。 該單一工作是完成集合中的第一項工作。 先完成的工作會指派給 finishedTaskfinishedTask 的型別是 TResult 為整數的 Task<TResult>,因為這是 ProcessURLAsync 的傳回型別。

    ' ***Call WhenAny and then await the result. The task that finishes
    ' first is assigned to finishedTask.
    Dim finishedTask As Task(Of Integer) = Await Task.WhenAny(downloadTasks)
    
  5. 在此範例中,您只想要知道先完成的工作。 因此,請使用 CancellationTokenSource.Cancel 取消剩餘的工作。

    ' ***Cancel the rest of the downloads. You just want the first one.
    cts.Cancel()
    
  6. 最後,等候 finishedTask 擷取所下載內容的長度。

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

執行程式數次,確認先完成不同的下載。

完整範例

下列程式碼是範例的完整 MainWindow.xaml.vb 或 MainWindow.xaml.cs 檔案。 星號會標記已針對此範例新增的項目。

請注意,您必須新增 System.Net.Http 的參考。

您可以從 Async Sample: Fine Tuning Your Application (非同步範例:微調應用程式) 下載專案。

' Add an Imports directive and a reference for System.Net.Http.
Imports System.Net.Http

' Add the following Imports directive for System.Threading.
Imports System.Threading

Class MainWindow

    ' Declare a System.Threading.CancellationTokenSource.
    Dim cts As CancellationTokenSource

    Private Async Sub startButton_Click(sender As Object, e As RoutedEventArgs)

        ' Instantiate the CancellationTokenSource.
        cts = New CancellationTokenSource()

        resultsTextBox.Clear()

        Try
            Await AccessTheWebAsync(cts.Token)
            resultsTextBox.Text &= vbCrLf & "Download complete."

        Catch ex As OperationCanceledException
            resultsTextBox.Text &= vbCrLf & "Download canceled." & vbCrLf

        Catch ex As Exception
            resultsTextBox.Text &= vbCrLf & "Download failed." & vbCrLf
        End Try

        ' Set the CancellationTokenSource to Nothing when the download is complete.
        cts = Nothing
    End Sub

    ' You can still include a Cancel button if you want to.
    Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)

        If cts IsNot Nothing Then
            cts.Cancel()
        End If
    End Sub

    ' Provide a parameter for the CancellationToken.
    ' Change the return type to Task because the method has no return statement.
    Async Function AccessTheWebAsync(ct As CancellationToken) As Task

        Dim client As HttpClient = New HttpClient()

        ' Call SetUpURLList to make a list of web addresses.
        Dim urlList As List(Of String) = SetUpURLList()

        '' Comment out or delete the loop.
        ''For Each url In urlList
        ''    ' GetAsync returns a Task(Of HttpResponseMessage).
        ''    ' Argument ct carries the message if the Cancel button is chosen.
        ''    ' Note that the Cancel button can cancel all remaining downloads.
        ''    Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)

        ''    ' Retrieve the website contents from the HttpResponseMessage.
        ''    Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync()

        ''    resultsTextBox.Text &=
        ''        vbCrLf & $"Length of the downloaded string: {urlContents.Length}." & vbCrLf
        ''Next

        ' ***Create a query that, when executed, returns a collection of tasks.
        Dim downloadTasksQuery As IEnumerable(Of Task(Of Integer)) =
            From url In urlList Select ProcessURLAsync(url, client, ct)

        ' ***Use ToArray to execute the query and start the download tasks.
        Dim downloadTasks As Task(Of Integer)() = downloadTasksQuery.ToArray()

        ' ***Call WhenAny and then await the result. The task that finishes
        ' 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 finishedTask
        resultsTextBox.Text &= vbCrLf & $"Length of the downloaded website:  {length}" & vbCrLf
    End Function

    ' ***Bundle the processing steps for a website into one async method.
    Async Function ProcessURLAsync(url As String, client As HttpClient, ct As CancellationToken) As Task(Of Integer)

        ' GetAsync returns a Task(Of HttpResponseMessage).
        Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)

        ' Retrieve the website contents from the HttpResponseMessage.
        Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync()

        Return urlContents.Length
    End Function

    ' Add a method that creates a list of web addresses.
    Private Function SetUpURLList() As List(Of String)

        Dim urls = New List(Of String) From
            {
                "https://msdn.microsoft.com",
                "https://msdn.microsoft.com/library/hh290138.aspx",
                "https://msdn.microsoft.com/library/hh290140.aspx",
                "https://msdn.microsoft.com/library/dd470362.aspx",
                "https://msdn.microsoft.com/library/aa578028.aspx",
                "https://msdn.microsoft.com/library/ms404677.aspx",
                "https://msdn.microsoft.com/library/ff730837.aspx"
            }
        Return urls
    End Function

End Class

' Sample output:

' Length of the downloaded website:  158856

' Download complete.

另請參閱