我正在尝试使用三个单独的 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));
}
使用它时,无论我做什么,三个 WebClient 中只有两个会完成。使用此代码,我得到两个消息框“CLIENT1”和“CLIENT2”,但“CLIENT3”从未出现。不会抛出异常或任何东西。什么都没发生。如果我颠倒 WebClients 的顺序,client3
并且client2
工作但不是client1
. 我尝试更改代码以使 WebClients 一次下载一个而不是异步:
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 也会冻结第三个文件。