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

  • Home
  • SEARCH
  • 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 4077950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:36:52+00:00 2026-05-20T17:36:52+00:00

I have to write a small app which downloads a few thousand files. Some

  • 0

I have to write a small app which downloads a few thousand files. Some of these files contain reference to other files that must be downloaded as part of the same process. The following code downloads the initial list of files, but I would like to download the others files as part of the same loop. What is happening here is that the loop completes before the first request come back. Any idea how to achieve this?

var countdownLatch = new CountdownEvent(Urls.Count);

string url;
while (Urls.TryDequeue(out url))
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.BeginGetResponse(
        new AsyncCallback(ar =>
        {
            using (HttpWebResponse response = (ar.AsyncState as HttpWebRequest).EndGetResponse(ar) as HttpWebResponse)
            {
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    string myFile = sr.ReadToEnd();

                    // TODO: Look for a reference to another file. If found, queue a new Url.
                }
            }
        }), webRequest);
}

ce.Wait();
  • 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-20T17:36:53+00:00Added an answer on May 20, 2026 at 5:36 pm

    One solution which comes to mind is to keep track of the number of pending requests and only finish the loop once no requests are pending and the Url queue is empty:

    string url;
    int requestCounter = 0;
    int temp;
    AutoResetEvent requestFinished = new AutoResetEvent(false);
    while (Interlocked.Exchange(requestCounter, temp) > 0 || Urls.TryDequeue(out url))
    {
        if (url != null)
        {
            Interlocked.Increment(requestCounter);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.BeginGetResponse(
                new AsyncCallback(ar =>
                {
                    try {
                        using (HttpWebResponse response = (ar.AsyncState as HttpWebRequest).EndGetResponse(ar) as HttpWebResponse)
                        {
                            using (var sr = new StreamReader(response.GetResponseStream()))
                            {
                                string myFile = sr.ReadToEnd();
    
                                // TODO: Look for a reference to another file. If found, queue a new Url.
                            }
                        }
                    } 
                    finally { 
                        Interlocked.Decrement(requestCounter); 
                        requestFinished.Set();
                    }
                }), webRequest);
        }
        else
        {
            // no url but requests are still pending
            requestFinished.WaitOne();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently starting porting a small app of mine to Facebook as a
I have an Android app that communicates with a server via asynchronous socket connection
I have to write an application for iphone that tracks the movement of the
I'm using QI and Phoenix, and I want to write a small grammar that
I have a variety of small C# winforms applications (though this should apply equally
We have a ten-year-old ASP application that we are considering planning an update for.
If i have an ImageView that fills the screen. The ImageView background is set
I'm writing a small networking program in C++. Among other things it has to
I am trying to write a small formatted test in RTF using the C#
I've been trying to write a small file server. I got it to the

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.