HELP with Webclient and simultaneous threads

E

Ezequiel

Hi,
I´m coding an application that is able to download various mp3 files
at the same time.
For each mp3 download i´m using a thread. Each thread uses
System.net.WebClient.
If i try to download 3 files at the same time, 2 of them starts
downloading (i see in the explorer the size incresing with the time)
however the third mp3 never starts downloading and after some seconds,
application throws a timeout exception in the client.OpenRead(URL);
line.
Below is the code of the thread:

try {
// Get HTML data
WebClient client = new WebClient();

Stream data = client.OpenRead(URL); // HERE HAPPENS
THE EXCEPTION
BinaryReader reader = new BinaryReader(data);
byte[] str = new byte[1024];
int intByteLeidos;



BinaryWriter BWEscritorBin = new BinaryWriter(new
FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.None));
try {
intByteLeidos = reader.Read(str, 0, 1024);
while (intByteLeidos>0) {
/* if (prgBar.Maximum > 100)
{
prgBar.Value += intByteLeidos;
}*/
BWEscritorBin.Write(str,0,intByteLeidos);
intByteLeidos = reader.Read(str, 0, 1024);

}
data.Close();
return 1;
} catch (IOException expIO) {

System.Windows.Forms.MessageBox.Show(expIO.Message,"Exception");
data.Close();
return 0;
}
} catch (WebException exp) {
System.Windows.Forms.MessageBox.Show(exp.Message,
"Exception");
return 0;
}
}

}


Any help will be really appreciated.

Ezequiel Naftali
([email protected])
 
J

Jon Skeet [C# MVP]

Ezequiel said:
I´m coding an application that is able to download various mp3 files
at the same time.
For each mp3 download i´m using a thread. Each thread uses
System.net.WebClient.
If i try to download 3 files at the same time, 2 of them starts
downloading (i see in the explorer the size incresing with the time)
however the third mp3 never starts downloading and after some seconds,
application throws a timeout exception in the client.OpenRead(URL);
line.

If they're all going to the same host, you'll be limited by the number
of connections. Try setting ServicePointManager.DefaultConnectionLimit
- I don't know if that's actually the appropriate place, but it could
well be.
 
R

Robson Siqueira

Jon,

They use the same connection limit as Internet Explorer, by nature. Why
don't you try a basic HTTPRequest?

Rgs,

Ezequiel said:
I´m coding an application that is able to download various mp3 files
at the same time.
For each mp3 download i´m using a thread. Each thread uses
System.net.WebClient.
If i try to download 3 files at the same time, 2 of them starts
downloading (i see in the explorer the size incresing with the time)
however the third mp3 never starts downloading and after some seconds,
application throws a timeout exception in the client.OpenRead(URL);
line.

If they're all going to the same host, you'll be limited by the number
of connections. Try setting ServicePointManager.DefaultConnectionLimit
- I don't know if that's actually the appropriate place, but it could
well be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top