Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 933849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:53:57+00:00 2026-05-15T20:53:57+00:00

I have written a method that downloads some files, and now I’m trying to

  • 0

I have written a method that downloads some files, and now I’m trying to make it download up to 5 files in parallel, and for the rest to wait for the previous ones to finish.
I am using a ManualResetEvent for this, but when i include the syncronisation part it doesn’t download anything anymore (without it it works).

Here is the code of the methods:

    static readonly int maxFiles = 5;
    static int files = 0;
    static object filesLocker = new object();
    static System.Threading.ManualResetEvent sync = new System.Threading.ManualResetEvent(true);

    /// <summary>
    /// Download a file from wikipedia asynchronously
    /// </summary>
    /// <param name="filename"></param>
    public void DoanloadFileAsync(string filename)
    {
        ...
        System.Threading.ThreadPool.QueueUserWorkItem(
            (o) =>
            {
                bool loop = true;
                while (loop)
                    if (sync.WaitOne())
                        lock (filesLocker)
                        {
                            if (files < maxFiles)
                            {
                                ++files;
                                if (files == maxFiles)
                                    sync.Reset();
                                loop = false;
                            }
                        }
                try
                {
                    WebClient downloadClient = new WebClient();
                    downloadClient.OpenReadCompleted += new OpenReadCompletedEventHandler(downloadClient_OpenReadCompleted);
                    downloadClient.OpenReadAsync(new Uri(url, UriKind.Absolute));
                    //5 of them do get here
                }
                catch
                {
                    lock (filesLocker)
                    {
                        --files;
                        sync.Set();
                    }
                    throw;
                }
            });
    }

    void downloadClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        try
        {
            //but none of the 5 get here
            ...Download logic... //works without the ManualResetEvent
        }
        finally
        {
            lock (filesLocker)
            {
                --files;
                sync.Set();
            }
        }
    }

Am I doing something wrong?

It is written with Silverlight 4 for Windows Phone 7.

Edit: There is no Semaphore or SemaphoreSlim in Silverlight 4.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:53:58+00:00Added an answer on May 15, 2026 at 8:53 pm

    What I meant in my comment was while use a slow lock when you can use Interlocked. Also it will be way more performant this way.

    At most 5 downloads active in parallel:

    public class Downloader
    {
     private int fileCount = 0;
     private AutoResetEvent sync = new AutoResetEvent(false);
    
     private void StartNewDownload(object o)
     {
      if (Interlocked.Increment(ref this.fileCount) > 5) this.sync.WaitOne();
    
      WebClient downloadClient = new WebClient();
      downloadClient.OpenReadCompleted += downloadClient_OpenReadCompleted;
      downloadClient.OpenReadAsync(new Uri(o.ToString(), UriKind.Absolute));
     }
    
     private void downloadClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
     {
      try
      {
       // Download logic goes here.
      }
      catch {}
      finally
      {
       this.sync.Set();
       Interlocked.Decrement(ref this.fileCount);
      }
     }
    
     public void Run()
     {
      string o = "url1";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
      Thread.Sleep(100);
    
      o = "url2";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
    
      o = "url3";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
      Thread.Sleep(200);
    
      o = "url4";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
    
      o = "url5";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
    
      o = "url6";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
    
      o = "url7";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
      Thread.Sleep(200);
    
      o = "url8";
      System.Threading.ThreadPool.QueueUserWorkItem(this.StartNewDownload, o);
      Thread.Sleep(400);
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm refactoring some code and I have written a method that modifies a Dictionary
I have written an O/R database wrapper that generates some wrapper methods for stored
My target: download files in parallel, and when file downloads complete, I get notifications.
I have recently written some complex ruby script that eventually fails with segfaults in
I have written a small tool for our customers that downloads the latest stable
I have list of files stored inside arraylist that I need to download in
I have written the following method that prevents all keys from being pressed: private
I have written a REST server based on RESTEasy . In some of the
I have written a method that accepts a TextWriter as an argument (Usually Console.Out,
I have written a method to calculate a given percentile for a set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.