我尝试验证我的图像网址以查看它们是否有效。我有这么多人,需要几个小时才能完成这项任务。因此,我决定以异步方式进行。我想知道下面的代码是否有很大的区别或优势。使用Parallel.ForEach与/或异步/等待
我的主要功能是:使用异步
Async Function testUrl_async(ByVal myImageurl As String) As Task(Of Boolean)
myHttpResponse = Await myHttpClient.GetAsync(myImageurl)
If myHttpResponse.IsSuccessStatusCode Then
mySuccess = True
Else
mySuccess = False
End If
Return mySuccess
End Function
Function testUrl(ByVal myImageurl As String) As Boolean
myHttpResponse = myHttpClient.GetAsync(myImageurl)
If myHttpResponse.IsSuccessStatusCode Then
mySuccess = True
Else
mySuccess = False
End If
Return mySuccess
End Function
1)等待。使用利用并行的foreach和asnyc并行的foreach
Parallel.ForEach(myImages,
Sub(myImage)
testUrl(pictureComponent.websiteShop.hqpatronen, myImageUrl)
'some code
End Sub)
3)
For Each myImage In myImages
Dim result=await testUrl_async(myImageUrl).Result
'some code
Next
2)/等待
Parallel.ForEach(myImages,
Sub(myImage)
await testUrl_async(pictureComponent.websiteShop.hqpatronen, myImageUrl)
'some code
End Sub)
第三个可能是最好的解决方案,但它不会让我在ForEach
内呼叫Await
/Async
。
如果我用的是第二个,testurl
函数具有异步HTTP调用,但不能与Await
,thereofore它与异常消息崩溃:
[TaskCanceledException:一个任务被取消]
上线myHttpClient.GetAsync
。我猜测它会抛出这个异常,因为ForEach
已经结束,请求取消,但httpclient还没有完成它的工作。如果这可能是最好的解决方案,我该如何处理?
或者任何其他解决方案,使我的工作更快。
这可能是TPL数据流,它提供与异步支持粗糙Parallel.ForEach功能良好的任务。 –
它是新的,只支持4.5及更高版本? – batmaci
@batmaci就像'async/await',是的 –