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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:08:16+00:00 2026-06-09T17:08:16+00:00

I am running a BackgroundWorker thread to do a time consuming task. The time

  • 0

I am running a BackgroundWorker thread to do a time consuming task. The time consuming task is in another class. I need to pass the progress being made on this separate class running on BackgroundWorker back to the Main Form1 class. I am not sure how to approach this. Please provide suggestions. Thank you in advance.

    **// Main Form1 UI Class**    

    public void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        //e.Argument always contains whatever was sent to the background worker
        // in RunWorkerAsync. We can simply cast it to its original type.
        DataSet ds = e.Argument as DataSet;
        this.createje.ProcessData(this.ds);
    }

    private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        this.progressBar1.Minimum = 0;
        this.progressBar1.Maximum = CreateJE.max;
        this.progressBar1.Value = e.Recno;
    }

    **//Other Class called CreateJE**

    public void ProcessData(DataSet ds)
    {
        //Do time consuming task...
     for (int i = 1; i <= 10; i++)
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);

                **//How do I report progress back to the Main UI?** 
                //worker.ReportProgress(i * 10);
            }
        }
    }
  • 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-09T17:08:18+00:00Added an answer on June 9, 2026 at 5:08 pm

    The cleanest, and most extendable, solution is probably to have your ProcessData() method raise an event, which the BackgroundWorker is listening for. This way ProcessData() doesn’t depend on having a BackgroundWorker as a caller. (You would also need to make a way of canceling out of ProcessData()). You can even re-use the ProgressChangedEventArgs if you want. For example (not tested but you get the idea?):

    partial class Form1 { 
        public void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) {
            //e.Argument always contains whatever was sent to the background worker 
            // in RunWorkerAsync. We can simply cast it to its original type.
            DataSet ds = (DataSet)e.Argument;
            var bgw = (BackgroundWorker)sender;
    
            var eh = new ProgressChangedEventHandler((o,a) => bgw.ReportProgress(a.ProgressPercentage));
            createje.ProgressChanged += eh;
            this.createje.ProcessData(this.ds));
            createje.ProgressChanged -= eh; //necessary to stop listening
        }
    
        private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Minimum = 0;
            this.progressBar1.Maximum = CreateJE.max;
            this.progressBar1.Value = e.ProgressPercentage;
        }
    
    }
    
    partial class CreateJE { 
    
        public event ProgressChangedEventHandler ProgressChanged; 
        protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
        { 
            var hand = ProgressChanged; 
            if(hand != null) hand(this, e);
        }
    
        public void ProcessData(DataSet ds)
        {
            for(int i = 1; i <= 10; i++)
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);
    
                var e = new ProgressChangedEventArgs(i * 10, null);
            }
        }
    
    }
    

    The quick and dirty way is to just pass the BackgroundWorker as a parameter to ProcessData(). This is IMHO rather ugly, though, ties you down to using only BackgroundWorkers, and also forces you to define the BackgroundWorker in one place (the main form class) and the returned values of ReportProgress in another (the CreateJE class).

    You could also use a timer and report back progress every X ms, querying the CreateJE object for its progress. This seems in-line with the rest of your code. The hangup with this is it would make your CreateJE class not multi-thread-friendly.

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

Sidebar

Related Questions

I have a longer running multi-step process using BackgroundWorker and C#. I need to
I am running a background worker thread that takes a long time to run.
I have a backgroundWorker thread running. At the end I update the UI, minimize
I have a C# class, MyCommand, that is run using a backgroundWorker and thread
I have the same BackgroundWorker code piece in two simultaneously running applications. Will this
I am using 5 BackgroundWorker objects running at the same time for a certain
I'm using System.ComponentModel.BackgroundWorker to execute some longer running task that - in the end
I have a long running series of operations in a .NET 2.0 BackgroundWorker thread.
I am currently having an issue with BackgroundWorker running on Windows Server 2003. I
I have some code which is been running by a backgroundworker I'd like some

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.