Sorrow - Can't use semaphore with Webclient

  • Thread starter Thread starter jehugaleahsa
  • Start date Start date
Travis,

Unfortunately, it also tells us nothing.

How are you using the Semaphore with the WebClient?
 
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






- 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.
 

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

Back
Top