I'm developing a .NET4-based application that has to request third-party servers in order to get information from them. I'm using HttpClient to make these HTTP requests.
I have to create a hundred or a thousand requests in a short period of time. I would like to throttle the creation of these request to a limit (defined by a constant or something) so the other servers don't receive a lot of requests.
I've checked this link out that shows how to reduce the amount of tasks created at any time.
Here is my non-working approach:
// create the factory
var factory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(level));
// use the factory to create a new task that will create the request to the third-party server
var task = factory.StartNew(() => {
return new HttpClient().GetAsync(url);
}).Unwrap();
Of course, the problem here is that even that one task at the time is created, a lot of requests will be created and processed at the same time, because they run in another scheduler. I could not find the way to change the scheduler to the HttpClient.
How should I handle this situation? I would like limit the amount of request created to a certain limit but do not block waiting for these request to finish.
Is this possible? Any ideas?
foreach
loop, or something like that? – Douty