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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:31:52+00:00 2026-06-07T18:31:52+00:00

I have a C# program that currently downloads data from several sites synchronously after

  • 0

I have a C# program that currently downloads data from several sites synchronously after which the code does some work on the data I’ve downloaded. I am trying to move this to do my downloads asynchronously and then process the data I’ve downloaded. I am having some trouble with this sequencing. Below is a snapshot of code I am using:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Started URL downloader");
        UrlDownloader d = new UrlDownloader();
        d.Process();
        Console.WriteLine("Finished URL downloader");

        Console.ReadLine();
    }
}

class UrlDownloader
{
    public void Process()
    {
        List<string> urls = new List<string>() { 
            "http://www.stackoverflow.com", 
            "http://www.microsoft.com", 
            "http://www.apple.com", 
            "http://www.google.com" 
        };

        foreach (var url in urls)
        {
            WebClient Wc = new WebClient();
            Wc.OpenReadCompleted += new OpenReadCompletedEventHandler(DownloadDataAsync);
            Uri varUri = new Uri(url);
            Wc.OpenReadAsync(varUri, url);
        }
    }

    void DownloadDataAsync(object sender, OpenReadCompletedEventArgs e)
    {
        StreamReader k = new StreamReader(e.Result);
        string temp = k.ReadToEnd();
        PrintWebsiteTitle(temp, e.UserState as string);
    }

    void PrintWebsiteTitle(string temp, string source)
    {
        Regex reg = new Regex(@"<title[^>]*>(.*)</title[^>]*>");
        string title = reg.Match(temp).Groups[1].Value;

        Console.WriteLine(new string('*', 10));
        Console.WriteLine("Source: {0}, Title: {1}", source, title);
        Console.WriteLine(new string('*', 10));
    }
}

Essentially, my problem is this. My output from above is:

Started URL downloader
Finished URL downloader
"Results of d.Process()"

What I want to do is complete the d.Process() method and then return to the “Main” method in my Program class. So, the output I am looking for is:

Started URL downloader
"Results of d.Process()"
Finished URL downloader

My d.Process() method runs asynchronously, but I can’t figure out how to wait for all of my processing to complete before returning to my Main method. Any ideas on how to do this in C#4.0? I am not sure how I’d go about ‘telling’ my Process() method to wait until all it’s asynchronous activity is complete before returning to the Main method.

  • 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-07T18:31:54+00:00Added an answer on June 7, 2026 at 6:31 pm

    If you are on .NET>=4.0 you can use TPL

    Parallel.ForEach(urls, url =>
    {
        WebClient Wc = new WebClient();
        string page = Wc.DownloadString(url);
        PrintWebsiteTitle(page);
     });
    

    I would also use HtmlAgilityPack to parse the page instead of regex.

    void PrintWebsiteTitle(string page)
    {
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(page);
        Console.WriteLine(doc.DocumentNode.Descendants("title").First().InnerText);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that I am currently working on which is working
I have a program that downloads mails from my Gmail, i have selected the
We have an app that currently installs itself into 'program files\our app', and it
I currently have a basic chat program in c++ that uses WinSock2.h with UDP.
I have a program that gets a JSON from the server using getJSON and
I have a program I'm writing that downloads to files. The second file is
I have a script (KSH script to be precise) that downloads 3 files from
I have a lot of legacy code which I currently compile using an antiquated
I'm building an iPhone app that aggregates data from several different data sources and
I have written a program that consists of roughly 3000 lines of code. 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.