Home All Groups Group Topic Archive Search About

HELP with Webclient and simultaneous threads

Author
19 Dec 2006 10:14 PM
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
(enaft***@pisol.com.ar)

Author
19 Dec 2006 10:44 PM
Jon Skeet [C# MVP]
Ezequiel <ezequiel.naft***@gmail.com> wrote:
>  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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
20 Dec 2006 7:47 PM
Robson Siqueira
Jon,

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

Rgs,

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1ff27b8d454fd00b98d713@msnews.microsoft.com...
Ezequiel <ezequiel.naft***@gmail.com> wrote:
>  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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Bookmark and Share

Post Thread options