【发布时间】:2016-08-23 14:32:37
【问题描述】:
我正在使用来自 Microsoft 的 example,它使用 Async 和 Tasks 下载多个 URL 的数据。
我的要求是在 1 分钟内完成 200 个链接的下载,以便在第 2 分钟再次开始下载同一组 200 个 URL。我知道这在很大程度上取决于网络速度,并且在较小程度上取决于 CPU 功率,因为这不是 IO 绑定进程。
假设网络和 CPU 将支持此操作并且不会成为瓶颈,我实际上在任务一段时间后看到超时和取消异常。
因此,在同一个示例中,我可以将其更改为长时间运行的任务,以使任务不会超时吗?我知道TaskCreationOptions 枚举的使用和LongRunning 的使用。然而,问题是:
1)如何在创建以下示例中的任务和提供的链接时提供此参数?
2)LongRunning的定义是什么?这是否意味着每个任务将不再超时?
3)我可以通过其他方式明确设置无限超时吗?
基本上,我的要求是,如果特定 URL 的下载过程完成,它将再次触发相同 URL 的下载 - 这意味着将一遍又一遍地下载相同的 URL,因此该任务永远不会完成(MSDN 示例中的 URL 不是我要触发的 URL,还有其他 URL,其内容每分钟都会更改,因此我需要至少每分钟持续下载一次 URL。
也将上述示例链接中的代码粘贴到此处:
Dim cts As CancellationTokenSource
Dim countProcessed As Integer
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 & "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
Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
If cts IsNot Nothing Then
cts.Cancel()
End If
End Sub
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()
' ***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 ToList to execute the query and start the download tasks.
Dim downloadTasks As List(Of Task(Of Integer)) = downloadTasksQuery.ToList()
Await Task.WhenAll(downloadTasks)
'Ideally, this line should never be reached
Console.WriteLine("Done")
End Function
Async Function ProcessURLAsync(url As String, client As HttpClient, ct As CancellationToken) As Task(Of Integer)
Console.WriteLine("URL=" & url)
' GetAsync returns a Task(Of HttpResponseMessage).
Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)
' Retrieve the web site contents from the HttpResponseMessage.
Dim urlContents As Byte() = Await response.Content.ReadAsByteArrayAsync()
Interlocked.Increment(countProcessed)
Console.WriteLine(countProcessed)
Return urlContents.Length
End Function
Private Function SetUpURLList() As List(Of String)
Dim urls = New List(Of String) From
{
"http://msdn.microsoft.com",
"http://msdn.microsoft.com/en-us/library/hh290138.aspx",
"http://msdn.microsoft.com/en-us/library/hh290140.aspx",
"http://msdn.microsoft.com/en-us/library/dd470362.aspx",
"http://msdn.microsoft.com/en-us/library/aa578028.aspx",
"http://msdn.microsoft.com/en-us/library/ms404677.aspx",
"http://msdn.microsoft.com/en-us/library/ff730837.aspx",
"http://msdn.microsoft.com/en-us/library/hh290138.aspx",
"http://msdn.microsoft.com/en-us/library/hh290140.aspx"
'For space constraint I am not including the 200 URLs, but pls assume the above list contains 200 URLs
}
Return urls
End Function
【问题讨论】:
标签: .net vb.net parallel-processing async-await task-parallel-library