Wiele WebClients nie działa?
Próbuję pobrać trzy pliki z trzema oddzielnymi aplikacjami WebClients. Używam tego:
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));
}
Używając tego, bez względu na to, co robię, tylko dwa z trzech WebClientów zakończą się. Korzystając z tego kodu, otrzymuję dwa okienka wiadomości „KLIENT1” i „KLIENT2”, ale „KLIENT3” nigdy się nie pojawia. Nie są wyrzucane wyjątki ani nic. Nic się nie dzieje. Jeśli odwrócę kolejność WebClients,client3
iclient2
pracować, ale nieclient1
. Próbowałem zmienić kod, aby WebClients pobierał pojedynczo, a nie asynchronicznie:
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");
Jednak program zawiesza się naStream str3 = client3.OpenRead(new Uri("http://www.example.com/file.exe?r=" + rand));
linia. Mającyclient1
pobieraj wszystkie pliki synchronicznie, zamiast tworzyć wiele WebClients, również zawiesza się na trzecim pliku.