I am trying to understand the intricacies of async / await in the C # completion and input / output ports on Windows by writing code to test my assumptions along the way.
From what I understand, calling WebClient.DownloadStringTaskAsync(...) will force the current thread to register an I / O operation with the I / O completion port (this is probably a bit vague, I still don't understand the details), this will create a Task<string> and it will continue to execute the code. At some point, he will encounter await for this task. At that moment, he will return from the current method (well, he will leave some area, Iām not sure if this area can be anything different from the method - I should probably check the created state machine to understand this part) As soon as the I / O operation is completed, the stream will be captured from the thread pool, it will be transferred the result of the I / O operation, and it will execute the rest of the just mentioned area.
I tried to check this behavior, but only got to this C # code before something seemed correct:
class Program { static void Main(string[] args) { ThreadPool.SetMaxThreads(30, 30); Console.WriteLine("Connection limit is {0}", ServicePointManager.DefaultConnectionLimit); for (int i = 0; i < 30; i++) { FetchAsync(i); } Console.WriteLine("Done starting requests"); Console.ReadKey(); } private async static void FetchAsync(int num) { WebClient wc = new WebClient(); string result = await wc.DownloadStringTaskAsync("http://localhost/slow/index/15"); Console.WriteLine("Done #{0}", num); } }
As you can see, I use WebClient to create 30 requests per web page (which, as I know, is slow and takes 15 seconds to respond). Running this code, I observe the following behavior: after 15 seconds, the first 10 requests are completed. After another 15 seconds, another 10 requests are completed and after another 15 seconds the remaining requests are completed. Thus, it seems that there are only 10 outstanding requests at the same time (using perfmon, I checked that the called web application has 10 current requests). Why is this? I was expecting 30 simultaneous requests since I set the maximum number of threads for the thread pool according to the code above.
This question with me ... makes me think that the ServicePointManager may have something to do with it, but since the DefaultConnectionLimit is only 2, I suppose there is no connection.
I understand that this is a rather long description of a fairly simple question. I hope that it will become easier for someone to indicate where my assumptions are wrong.
I run this on a machine with Windows 7, 64 bit.