Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to throttle concurrent Async webrequests
    primarykey
    data
    text
    <p>I often need to make a large number of webrequests, without overloading the network</p> <p>I currently do this by running synchronous requests in parallel, utilizing ThreadPool.SetMinThreads and MaxDegreeOfParallelism to exactly specify how many requests run concurrently</p> <p>Now this works just fine, but it feels wrong.</p> <p>I would really like to utilize async methods, but i cant work out how to limit the number of concurrent requests.</p> <p>A simplified example of my parallel way of doing this( using a webclient and no error handling for brevity):</p> <pre><code>Private Function SearchSitesForKeywordInParallel(ByVal keyword As String, ByVal sites As String(), ByVal maxConcurrency As Integer) As String() Dim po As New ParallelOptions po.MaxDegreeOfParallelism = maxConcurrency Threading.ThreadPool.SetMinThreads(maxConcurrency, 2) Dim sitesContainingKeyword As New Concurrent.ConcurrentBag(Of String) Parallel.For(0, sites.Count, po, Sub(i) Dim wc As New Net.WebClient wc.Proxy = Nothing Dim pageSource As String = wc.DownloadString(sites(i)) If pageSource.Contains(keyword) Then sitesContainingKeyword.Add(sites(i)) End If End Sub) Return sitesContainingKeyword.ToArray End Function </code></pre> <p>This is a blocking function, which is what i require. Now i have tested the webclient.downloadStringAsync method in a regular for loop, and it will fire all the requests pretty much at once, overloading the network.</p> <p>What i would like to do is initially make X requests, then make new ones as each response comes back.</p> <p>I am fairly sure tasks is the way to go, and im positive a have read some very nice implementations in c#, but my c# experience is limited, and i have a hard time translating c# lambadas to vb.net.</p> <p>I am also limited to vs2010 and .net4, so the niceties of .net4.5 async await are not an option for me.</p> <p>Any help very much appreciated</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload