多个WebClient无法正常工作?

msb*_*sbg 4 c# networking webclient

我试图用三个单独的WebClient下载三个文件.我用这个:

    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));
    }
Run Code Online (Sandbox Code Playgroud)

使用它时,无论我做什么,三个WebClient中只有两个会完成.使用此代码,我得到两个消息框"CLIENT1"和"CLIENT2",但"CLIENT3"永远不会出现.不会抛出异常或任何异常.什么都没发生.如果我扭转Web客户的订单,client3client2工作,但不是client1.我尝试更改代码以使WebClient一次下载一个而不是异步下载:

        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");
Run Code Online (Sandbox Code Playgroud)

但是,程序冻结了Stream str3 = client3.OpenRead(new Uri("http://www.example.com/file.exe?r=" + rand));.有client1下载的所有文件同步,而不是创建多个Web客户也冻结的第三个文件.

Kir*_*kiy 5

我认为你遇到的是两个问题的组合.第一个是WebRequest默认情况下并发连接数限制为2.你可以通过创建一个派生自WebClient并重写GetWebRequest方法的类来改变它,如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我看到的第二个问题是你在Stream调用时没有关闭/处理返回的内容OpenRead,所以看起来两个请求都没有完成,直到垃圾收集器决定启动并为你关闭这些流.