多个WebClient无法正常工作? [英] Multiple WebClients not working?
问题描述
我正在尝试使用三个单独的WebClient下载三个文件.我用这个:
I am trying to download three files with three separate WebClients. I use this:
void client1_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
MessageBox.Show("CLIENT1");
}
void client2_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
MessageBox.Show("CLIENT2");
}
void client3_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
MessageBox.Show("CLIENT3");
}
private void mwindow_Loaded(object sender, RoutedEventArgs e)
{
string rand = new Random().Next().ToString();
WebClient client1 = new WebClient();
client1.OpenReadCompleted += client1_OpenReadCompleted;
client1.OpenReadAsync(new Uri("http://www.example.com/file.exe?r=" + rand));
WebClient client2 = new WebClient();
client2.OpenReadCompleted += client2_OpenReadCompleted;
client2.OpenReadAsync(new Uri("http://www.example.com/file.exe?r=" + rand));
WebClient client3 = new WebClient();
client3.OpenReadCompleted += client3_OpenReadCompleted;
client3.OpenReadAsync(new Uri("http://www.example.com/file.exe?r=" + rand));
}
使用此功能时,无论我做什么,三个WebClient中只有两个会完成.使用此代码,我得到两个消息框"CLIENT1"和"CLIENT2",但是"CLIENT3"从不出现.没有引发异常或任何异常.没发生什么事.如果我颠倒了WebClient的顺序,则 client3
和 client2
起作用,但 client1
无效.我尝试更改代码以使WebClient一次下载一个,而不是异步下载:
When using this, no matter what I do, only two out of the three WebClients will finish. Using this code, I get two message boxes "CLIENT1", and "CLIENT2", but "CLIENT3" never appears. No exceptions are thrown or anything. Nothing happens. If I reverse the order of the WebClients, client3
and client2
work but not client1
. I have tried changing the code to make the WebClients download one at a time instead of asynchronously:
WebClient client1 = new WebClient();
Stream str1 = client1.OpenRead(new Uri("http://www.example.com/file.exe?r=" + rand));
MessageBox.Show("CLIENT1");
WebClient client2 = new WebClient();
Stream str2 = client2.OpenRead(new Uri("http://www.example.com/file.exe?r=" + rand));
MessageBox.Show("CLIENT2");
WebClient client3 = new WebClient();
Stream str3 = client3.OpenRead(new Uri("http://www.example.com/file.exe?r=" + rand));
MessageBox.Show("CLIENT3");
但是,程序冻结在 Stream str3 = client3.OpenRead(new Uri("http://www.example.com/file.exe?r=" + rand));
线. client1
同步下载所有文件而不是创建多个WebClient也会冻结第三个文件.
However, the program freezes on the Stream str3 = client3.OpenRead(new Uri("http://www.example.com/file.exe?r=" + rand));
line. Having client1
download all the files synchronously instead of creating multiple WebClients also freezes on the third file.
推荐答案
我认为您遇到的是两个问题的结合.第一个是默认情况下,并发 WebRequest
连接的数量限制为2.您可以通过创建从 WebClient
派生的类并覆盖 GetWebRequest
方法来更改此方法,如下所示:
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
{
/// <summary>
/// Gets or sets the maximum number of concurrent connections (default is 2).
/// </summary>
public int ConnectionLimit { get; set; }
/// <summary>
/// Creates a new instance of ExtendedWebClient.
/// </summary>
public ExtendedWebClient()
{
this.ConnectionLimit = 2;
}
/// <summary>
/// Creates the request for this client and sets connection defaults.
/// </summary>
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
if (request != null)
{
request.ServicePoint.ConnectionLimit = this.ConnectionLimit;
}
return request;
}
}
我看到的第二个问题是,您没有关闭/处理调用 OpenRead
时返回的 Stream
,因此这两个请求似乎没有完成,直到垃圾收集器决定为您打开并关闭这些流为止.
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.
这篇关于多个WebClient无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!