I am trying to download three files with three separate WebClients. I use this:
void client1_OpenReadCompleted(object sender, OpenReadCompletedEventArgs
I think what you're experiencing is a combination of two issues. The first one being that the number of concurrent WebRequest
connections is limited to 2 by default. You can change that by creating a class derived from WebClient
and overriding the GetWebRequest
method like so:
public class ExtendedWebClient : WebClient
{
///
/// Gets or sets the maximum number of concurrent connections (default is 2).
///
public int ConnectionLimit { get; set; }
///
/// Creates a new instance of ExtendedWebClient.
///
public ExtendedWebClient()
{
this.ConnectionLimit = 2;
}
///
/// Creates the request for this client and sets connection defaults.
///
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
if (request != null)
{
request.ServicePoint.ConnectionLimit = this.ConnectionLimit;
}
return request;
}
}
The second problem I see is that you're not closing/disposing of the Stream
returned when you call OpenRead
, so it would appear that the two requests do not complete until such time when the Garbage Collector decides to kick in and close those streams for you.