// let's say there is a list of 1000+ URLs
string[] urls = { "http://google.com", "http://yahoo.com", ... };
// now let's send HTTP requests to each of these URLs in parallel
urls.AsParallel().ForAll(async (url) => {
var client = new HttpClient();
var html = await client.GetStringAsync(url);
});
Run Code Online (Sandbox Code Playgroud)
这是问题所在,它会同时启动1000多个Web请求.有没有一种简单的方法来限制这些异步http请求的并发数量?这样在任何给定时间都不会下载超过20个网页.如何以最有效的方式做到这一点?
我尝试验证我的图片网址,看看它们是否有效.我有这么多人,完成这项任务需要几个小时.因此,我决定异步进行.我想知道我的代码是否有任何重大差异或优势,如下所示.
我的主要职能是:
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
Run Code Online (Sandbox Code Playgroud)
1)使用异步等待.
For Each myImage In myImages
Dim result=await testUrl_async(myImageUrl).Result
'some code
Next
Run Code Online (Sandbox Code Playgroud)
2)使用平行foreach
Parallel.ForEach(myImages,
Sub(myImage)
testUrl(pictureComponent.websiteShop.hqpatronen, myImageUrl)
'some code
End Sub)
Run Code Online (Sandbox Code Playgroud)
3)使用并行foreach和asnyc/await
Parallel.ForEach(myImages, …Run Code Online (Sandbox Code Playgroud)