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 8932975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:25:38+00:00 2026-06-15T09:25:38+00:00

I need to download a few text from different urls, then I am using

  • 0

I need to download a few text from different urls, then I am using the CountDownEvent to handle the number of times that my event Donwnload is completed, but the thing is my CountDownEvent never is set to Zero and this remains waiting.

Any idea what is wrong with this code?

namespace WebApplication.AsyncCall
{
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Threading;

    public partial class _Default : System.Web.UI.Page
    {
        private CountdownEvent countDown = null;
        public CountdownEvent CountDown
        {
            get
            {
                if (this.countDown == null)
                {
                    this.countDown = new CountdownEvent(1);
                }

                return this.countDown;
            }
        }

        private List<string> text = null;
        public List<string> Text
        {
            get
            {
                if (this.text == null)
                {
                    this.text = new List<string>();
                }

                return this.text;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> rssSources = new List<string>();

            rssSources.Add(@"http://news.yahoo.com/rss/entertainment");
            rssSources.Add(@"http://go.microsoft.com/fwlink/?linkid=84795&clcid=409");

            foreach (string uri in rssSources)
            {
                this.CountDown.AddCount();
                LoadSources(uri);
            }

            this.CountDown.Signal();
            this.CountDown.Wait();
        }

        private void LoadSources(string uri)
        {
            WebClient client = new WebClient();
            client.DownloadStringAsync(new Uri(uri, UriKind.Absolute));

            client.DownloadStringCompleted += (s, a) =>
            {
                if (a.Error == null && !a.Cancelled)
                {
                    this.Text.Add(a.Result);
                    this.CountDown.Signal();
                }
            };
        }
    }
}
  • 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-06-15T09:25:39+00:00Added an answer on June 15, 2026 at 9:25 am

    I finally figure out how to solve my problem, the thing is despite that I am firing my download event asynchronous it seems they still are executed on the main thread, which mean that this.CountDown.Wait() is being invoked before that any download is completed then my this.CountDown is not being signaled therefore this.CountDown never is set to zero and this remains waiting.

    Here what I did:

    Into the foreach I replaced the call to the method LoadSources(uri) by a ThreadPool.QueueUserWorkItem which queues a method for execution. The method executes when a thread pool thread becomes available.

    ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSources), (object)uri);
    

    I also had to change the LoadSources method to fit my adjustments.

    private void LoadSources(object uri)
    {
        WebClient client = new WebClient();
        client.DownloadStringAsync(new Uri(uri.ToString(), UriKind.Absolute));
    
        client.DownloadStringCompleted += (s, a) =>
        {
            lock (thisLock)
            {
                try
                {
                    if (a.Error == null && !a.Cancelled)
                    {
                        this.Text.Add(a.Result);
                    }
                }
                finally
                {
                    this.CountDown.Signal();
                } 
            }
        };
    }
    

    And as you can see I added a lock statement to avoid that two or more threads try to call this.Text.Add simultaneously

    Before that I just declared a private object to lock on.

    private Object thisLock = new Object();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to download one file from an url that i give from an
Hi I'm in need to download a file from a site that requires Forms
I have a 1.3GB text file that I need to extract some information from
I'm using HttpClient together with HttpGet to download a few websites. They're detecting that
I only need to download the first few kilobytes of a file via HTTP.
I need to download file from FTP server and make some changes on it
I need to download an image from some URL to my server. However, my
I need to download images from a website, and I have the login name
I need to download Gmail emails using php api. there APIs for google. i
I've seen recently a few times when companies offer either a download link or

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.