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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:20:44+00:00 2026-06-12T23:20:44+00:00

I’ve a question on how the backgroundworker would actually work. I’m not too sure

  • 0

I’ve a question on how the backgroundworker would actually work. I’m not too sure how it would work with different methods.

So for example I have the following code (adapted from http://broadcast.oreilly.com/2010/06/understanding-c-using-backgrou.html for illustration purposes):

private void getDateTime()
{
    DateTime startTime = DateTime.Now;
    double value = Math.E;
    while (DateTime.Now < startTime.AddMilliseconds(100)) 
    {
      value /= Math.PI;
      value *= Math.Sqrt(2);
    }
}

private backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for(int i=0; i <= 100; i++)
    {
        getDateTime();
        backgroundWorker1.ReportProgress(i);
    }
}

This would continually calculate the DateTime using the backgroundworker thread. Now I’m assuming that the for-loop within the backgroundworker’s DoWork method would run the calculation, report the progress, and then reiterate the loop (please do correct me if I’ve misunderstood).

Now assuming I have another method, which parses a (presumably large) DataTable and returns another DataTable, using it in the backgroundworker would mean I’d be parsing the DataTable over and over (theoretically. again, correct me if I’m wrong)? If this is how the program would flow, is there a more efficient method to achieve the DataTable parsing without having to reiterate the loop, but still being able to use backgroundworker?

This is intended for a Windows Form, and the UI tends to freeze up when I parse the DataTable(s). I’ve heard that backgroundworker could help me get around the freezing of the UI, but I find it highly inefficient to have to re-parse the entire DataTable over, and over. However I’m not sure of any other way I can report progress without a for-loop either.

EDIT:

I’ve figured out a temporary solution. It works, but I still have no way of reporting progress.

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int rows = (int)e.Argument;
        int count = 0;
        t3 = runComparison();
        for (int i = 0; i <= rows; i++)
        {
            count++;
            int current = count / rows;
            backgroundWorker1.ReportProgress(current * 100);
        }
        e.Result = t3;
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            dataGridView1.DataSource = e.Result;
        }
    }

I’m aware my for-loop doesn’t do anything. However, the “runComparison” already consumes a lot of time. As such, progress doesn’t get reported until an entire run-through if it’s in the for-loop. Alternatively, maybe I could use a label or messagebox instead of the progress bar. Not too sure how I’m supposed to achieve this.

  • 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-12T23:20:45+00:00Added an answer on June 12, 2026 at 11:20 pm

    I’ve come up with a rather inelegant solution. But it satisfies the requirement for user-friendliness so… Whatever works 😛

    Thanks for all the feedback, I appreciate it.

    Anyway, here’s the code:

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker1.ReportProgress(1); //to invoke ProgressChanged
            t3 = runComparison(); //run intensive method
            e.Result = t3; //return DataTable
        }
    
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Visible = true; //display a label
            textBoxProgress.Visible = true; //display the progress bar
            //change the progress bar style
            //the Marquee style is constantly running, so it could show users the process is working
            //not too sure how to explain it, you'll have to try it out. 
            progressBar1.Style = ProgressBarStyle.Marquee; 
        }
    
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //reset the display and update the DataGridView
                progressBar1.Visible = false;
                textBoxProgress.Visible = false;
                progressBar1.Style = ProgressBarStyle.Blocks;
                dataGridView1.DataSource = e.Result;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't

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.