Home All Groups Group Topic Archive Search About

Sorrow - Can't use semaphore with Webclient

Author
29 Nov 2007 3:19 AM
jehugaleahsa@gmail.com
The title says it all. :-(

~Travis

Author
29 Nov 2007 4:36 AM
Nicholas Paldino [.NET/C# MVP]
Travis,

    Unfortunately, it also tells us nothing.

    How are you using the Semaphore with the WebClient?


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

<jehugalea***@gmail.com> wrote in message
Show quote
news:da1f114a-5cb5-4736-84f8-6b75b68ed204@o6g2000hsd.googlegroups.com...
> The title says it all. :-(
>
> ~Travis
Author
29 Nov 2007 12:18 PM
Peter Bromberg [C# MVP]
There is nothing about the WebClient class that prevents you from using
Semaphore in your code.  As Nick indicates, you really haven't told us much.

--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com



Show quote
"jehugalea***@gmail.com" wrote:

> The title says it all. :-(
>
> ~Travis
>
Author
29 Nov 2007 2:51 PM
jehugaleahsa@gmail.com
On Nov 29, 5:18 am, Peter Bromberg [C# MVP]
<pbromb...@yahoo.NoSpamMaam.com> wrote:
Show quote
> There is nothing about the WebClient class that prevents you from using
> Semaphore in your code.  As Nick indicates, you really haven't told us much.
>
> --Peter
> "Inside every large program, there is a small program trying to get out."http://www.eggheadcafe.comhttp://petesbloggerama.blogspot.comhttp://www.blogmetafinder.com
>
>
>
> "jehugalea***@gmail.com" wrote:
> > The title says it all. :-(
>
> > ~Travis- Hide quoted text -
>
> - Show quoted text -


Sorry, I wasn't able to put the code snippet in. I was rushed last
night.

This is somewhat like what my code looks like:

        static void Main()
        {
            Semaphore semaphore = new Semaphore(1, 1);
            int i = 0;
            foreach (WebClient client in GetNextInt())
            {
                semaphore.WaitOne();
                client.DownloadFileCompleted
                    += new AsyncCompletedEventHandler(
                    delegate(object sender, AsyncCompletedEventArgs e)
                    {
                        semaphore.Release();
                    });
                client.DownloadFileAsync(new Uri("http://
www.google.com"), @"C:\temp" + i + ".html");
                ++i;
            }
        }

        static IEnumerable<WebClient> GetNextInt()
        {
            for (int i = 0; i != 10; ++i)
            {
                Thread.Sleep(new Random().Next(1000, 5000));
                yield return new WebClient();
            }
        }

When my code got to the last download, it stopped prematurely. So the
last file in this example always had a file of 0 bytes. Which means my
application is stopping even though there are downloads currently in
progress. I would have thought a thread would have prevented that.

Is there anyway to prevent this?

By the way, I thought my application wasn't working at all last night
because I only had one Web Client.

AddThis Social Bookmark Button