共用方式為


在一段時間後取消非同步工作 (Visual Basic)

如果不想等候作業完成,則可以使用 CancellationTokenSource.CancelAfter 方法,在一段時間之後取消非同步作業。 這個方法排定取消未在 CancelAfter 運算式所指定之2期間內完成的任何相關工作。

這個範例會新增至取消一項非同步工作或工作清單 (Visual Basic) 中所開發的程式碼來下載網站清單,以及顯示每個網站內容的長度。

注意

若要執行範例,您必須在電腦上安裝 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. 在方案總管中,開啟 CancelAfterTime 專案的捷徑功能表,然後選擇 [設定為啟始專案]

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

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

  6. 執行程式數次,確認輸出可能會顯示所有網站、沒有網站或某些網站的輸出。

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

建置範例

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

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

若要指定將工作標記為取消之前的最長時間,請將 CancelAfter 呼叫新增至 startButton_Click,如下列範例所示。 新增的項目會以星號標記。

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

    ' Instantiate the CancellationTokenSource.
    cts = New CancellationTokenSource()

    resultsTextBox.Clear()

    Try
        ' ***Set up the CancellationTokenSource to cancel after 2.5 seconds. (You
        ' can adjust the time.)
        cts.CancelAfter(2500)

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

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

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

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

執行程式數次,確認輸出可能會顯示所有網站、沒有網站或某些網站的輸出。 下列輸出是範例:

Length of the downloaded string: 35990.

Length of the downloaded string: 407399.

Length of the downloaded string: 226091.

Downloads canceled.

完整範例

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

請注意,您必須新增 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
            ' ***Set up the CancellationTokenSource to cancel after 2.5 seconds. (You
            ' can adjust the time.)
            cts.CancelAfter(2500)

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

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

        Catch ex As Exception
            resultsTextBox.Text &= vbCrLf & "Downloads 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()

        ' Process each element in the list of web addresses.
        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
    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 string: 35990.

' Length of the downloaded string: 407399.

' Length of the downloaded string: 226091.

' Downloads canceled.

另請參閱