我使用Microsoft的example,使用Async
和Tasks
下载多个网址的数据。
我的要求是在1分钟内完成200个链接的下载,以便第2分钟同一组200个网址将再次开始下载。我知道这在很大程度上取决于网络速度和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
答案 0 :(得分:2)
因此,问题在同一示例中,我可以将其更改为长时间运行的任务,以便任务不会超时吗?
任务本身不会超时。您可能看到的是HTTP请求超时。长时间运行的任务没有任何不同的超时语义。
我知道使用TaskCreationOptions枚举并使用LongRunning。
您还应该意识到它们几乎不会被使用。
您可能会收到超时,因为您的所有请求都在同一个网站上。尝试将ServicePointManager.DefaultConnectionLimit
设置为int.MaxValue
,也可以增加HttpClient.Timeout
。