0

Possible Duplicate:
How to limit the amount of concurrent async I/O operations?

Let say i have an string array

string[] domains = { "domain1.com", "domain2.com" ,..., "domain100.com"};

I want to write an app that use 5 threads to send request to those domains and save the response header to the same log file in order of started. For example:

Thread 1
domain1.com
Response header


Thread 2
domain2.com
Response header


Thread 3
domain3.com
Response header

Thread 4
domain4.com
Response header


Thread 5
domain5.com
Response header


Thread 1
domain6.com
Response header

Thread 2
domain7.com
Response header

I'm kind of new at programming multithread so hope you can help me out. Thanks a lot

Community
  • 1
  • 1
ByulTaeng
  • 1,269
  • 1
  • 21
  • 40
  • Why do you want to use 5 threads to do this? Async IO means that you don't need to think too much about threads as .net will schedule any callbacks in the ThreadPool. Do you simply want to limit to 5 concurrent downloads? – spender Oct 28 '12 at 01:29
  • And using the new `async` support in C# 5 would be far more efficient than multiple threads, and easier to get right since you don't have to deal with synchronization and potential race conditions. – Ben Voigt Oct 28 '12 at 01:30

2 Answers2

2

You don't need to care much how many thread created under the hood, thread pool with take care this for you with optimizing thread creation, your code might look like:

string[] domains = { "domain1.com", "domain2.com",...., "domain100.com" };

var client = new HttpClient();
var tasks = domains.Select(domain => client.GetAsync(domain)
                               .ContinueWith(task =>
                               {
                                   HttpResponseMessage response = task.Result;
                                   var headers = response.Headers;
                                   //Write to log file
                               }));

await Task.WhenAll(tasks);
cuongle
  • 74,024
  • 28
  • 151
  • 206
1

I would use RX extensions.

Paul Betts had a great overview of RX extensions and he solved exactly your problem on NDC conference earlier this year.

Video available on NDC website