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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:44:55+00:00 2026-06-07T08:44:55+00:00

This is my first steps into async/threading so apologies in advance. I need some

  • 0

This is my first steps into async/threading so apologies in advance. I need some advice on the best way to implement the following…

I have a windows form which is non-static containing a progress bar. I also have a static method ‘HttpSocket’ to manage async http downloads. Therefore, I can’t access the forms progress bar directly from a static method.

So I thought about using the backgroundWorker to run the job. However, because DoWork is also calling an async method, the backgroundWorker reports complete once all of the http requests are made but I want to update the progress bar based on when the http responses are received and data parsed.

One awful way around this I’ve come up with is as follows

private void buttonStartDownload_Click(object sender, EventArgs e)
{
  backgroundWorker1.RunWorkerAsync();
}

and place a while loop in backgroundWorker1_DoWork to compare requests/responses

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    //Trigger Asynchronous method from LoginForm
    DataExtract LoginForm = new DataExtract();
    LoginForm.DELogin();

    //Without While Loop backgroundWorker1 completes on http requests and not responses

    // Attempt to Monitor Progress of async responses using while loop
   // HttpSocket method logs RequestCount & ResponseCount

    while (HttpSocket.UriWebResponseCount < HttpSocket.UriWebRequestCount)
    {

        if (HttpSocket.UriWebResponseCount % updateInterval == 0) 
        {
            int myIntValue = unchecked((int)HttpSocket.UriWebResponseCount / HttpSocket.UriTotal);
            backgroundWorker1.ReportProgress(myIntValue);
        }

    }

}


private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Change the value of the ProgressBar to the BackgroundWorker progress.
    progressBar1.Value = e.ProgressPercentage;

}

However, I realise this is not the best way to do this as the While loop impacts performance and slows the async process which is normally fast without showing progress.
I’m after suggestions on the proper, most efficient way to accomplish this or offer alternate ways to update a Form progress bar from a separate async thread either with or without BackgroundWorker using C#4.0?

Thank you

O

  • 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-07T08:44:57+00:00Added an answer on June 7, 2026 at 8:44 am

    Without fully understanding the architecture of your request / response model, it looks as if the while loop in the background worker is essentially busy waiting.

    You can check the status of progress much less frequently by inserting a sleep operation at the top of the while loop, and I would also suggest removing the check to see if the response count is an integral value before reporting progress.

    while (HttpSocket.UriWebResponseCount < HttpSocket.UriWebRequestCount) 
    { 
        Thread.Sleep(250); // sleep for 250 ms before the next check
    
        int myIntValue = (int)Math.Floor((double)HttpSocket.UriWebResponseCount / HttpSocket.UriTotal); 
        backgroundWorker1.ReportProgress(myIntValue); 
    } 
    

    Hopefully the static properties HttpSocket.UriWebResponseCount and HttpSocket.UriWebRequestCount are being updated and read in a thread-safe fashion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my first steps into Lift I just started with this tutorial but it
This is the first time that I have attempted to add some localization into
Im moving first steps into JSF framework. I so make these jsp/bean : index.jsp
I'm taking my first baby steps into consuming web services. Here's my situation: I'm
Still making my first steps in Ruby (while dealing with some written code). I
I'm trying to implement something quite simple but I'm on my first steps in
I'm starting to make my first steps into CakePHP 1.3, and I'm having a
First off i'm new to Linux programming so apologies if this makes no sense
I am making my first tentative steps into MVC3 and have come across an
I'm doing my first steps in Puppet and ran into a problem. I've installed

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.