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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:32:20+00:00 2026-05-24T11:32:20+00:00

I have a number of classes that do stuff, typically step through a recordset

  • 0

I have a number of classes that do stuff, typically step through a recordset and call a webservice or two for each record.

At the moment this all runs in the GUI thread and hangs painting. First thought was to use a BackgroundWorker and implement a nice progress bar, handle errors, completion etc. All the nice things a Background worker enables.

As soon as the code hit the screen it started to smell. I was writing a lot of the background worker into each class, repeating most of the ProcessRows method in a bw_DoWork method and thinking there should be a better way, and it’s probably already been done.

Before I go ahead and reinvent the wheel is there a pattern or implementation for a class that seperates out the background worker? It would take classes that implement an interface such as ibackgroundable, but the classes could still be run standalone, and would require minimal change to implement the interface.

Edit: A simplified example requested by @Henk:

I have:

    private void buttonUnlockCalls_Click(object sender, EventArgs e)
    {
        UnlockCalls unlockCalls = new UnlockCalls();
        unlockCalls.MaxRowsToProcess = 1000;
        int processedRows = unlockCalls.ProcessRows();
        this.textProcessedRows.text = processedRows.ToString();
    }

I think I want:

    private void buttonUnlockCalls_Click(object sender, EventArgs e)
    {
        UnlockCalls unlockCalls = new UnlockCalls();
        unlockCalls.MaxRowsToProcess = 1000;

        PushToBackground pushToBackground = new PushToBackground(unlockCalls)
        pushToBackground.GetReturnValue = pushToBackground_GetReturnValue;
        pushToBackground.DoWork();
    }

    private void pushToBackground_GetReturnValue(object sender, EventArgs e)
    {
        int processedRows = e.processedRows;
        this.textProcessedRows.text = processedRows.ToString();
    }

I could go ahead and do this, but don’t want to reinvent.

The answer I’m looking for would along the lines of “Yes, Joe did a good implementation of that (here)” or “That’s a Proxy Widget pattern, go read about it (here)”

  • 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-24T11:32:21+00:00Added an answer on May 24, 2026 at 11:32 am

    Each operation needs to implement the following interface:

    /// <summary>
    /// Allows progress to be monitored on a multi step operation
    /// </summary>
    interface ISteppedOperation
    {
        /// <summary>
        /// Move to the next item to be processed.
        /// </summary>
        /// <returns>False if no more items</returns>
        bool MoveNext();
    
        /// <summary>
        /// Processes the current item
        /// </summary>
        void ProcessCurrent();
    
        int StepCount { get; }
        int CurrentStep { get; }
    }
    

    This seperates the enumeration of the steps from the processing.

    Here is a sample operation:

    class SampleOperation : ISteppedOperation
    {
        private int maxSteps = 100;
    
        //// The basic way of doing work that I want to monitor
        //public void DoSteppedWork()
        //{
        //    for (int currentStep = 0; currentStep < maxSteps; currentStep++)
        //    {
        //        System.Threading.Thread.Sleep(100);
        //    }
        //}
    
        // The same thing broken down to implement ISteppedOperation
        private int currentStep = 0; // before the first step
        public bool MoveNext()
        {
            if (currentStep == maxSteps)
                return false;
            else
            {
                currentStep++;
                return true;
            }
        }
    
        public void ProcessCurrent()
        {
            System.Threading.Thread.Sleep(100);
        }
    
        public int StepCount
        {
            get { return maxSteps; }
        }
    
        public int CurrentStep
        {
            get { return currentStep; }
        }
    
        // Re-implement the original method so it can still be run synchronously
        public void DoSteppedWork()
        {
            while (MoveNext())
                ProcessCurrent();
        }
    }
    

    This can be called from the form like this:

    private void BackgroundWorkerButton_Click(object sender, EventArgs eventArgs)
    {
        var operation = new SampleOperation();
    
        BackgroundWorkerButton.Enabled = false;
    
        BackgroundOperation(operation, (s, e) =>
            {
                BackgroundWorkerButton.Enabled = true;
            });
    }
    
    private void BackgroundOperation(ISteppedOperation operation, RunWorkerCompletedEventHandler runWorkerCompleted)
    {
        var backgroundWorker = new BackgroundWorker();
    
        backgroundWorker.RunWorkerCompleted += runWorkerCompleted;
        backgroundWorker.WorkerSupportsCancellation = true;
        backgroundWorker.WorkerReportsProgress = true;
    
        backgroundWorker.DoWork += new DoWorkEventHandler((s, e) =>
        {
            while (operation.MoveNext())
            {
                operation.ProcessCurrent();
    
                int percentProgress = (100 * operation.CurrentStep) / operation.StepCount;
                backgroundWorker.ReportProgress(percentProgress);
    
                if (backgroundWorker.CancellationPending) break;
            }
        });
    
        backgroundWorker.ProgressChanged += new ProgressChangedEventHandler((s, e) =>
        {
            var progressChangedEventArgs = e as ProgressChangedEventArgs;
            this.progressBar1.Value = progressChangedEventArgs.ProgressPercentage;
        });
    
        backgroundWorker.RunWorkerAsync();
    }
    

    I haven’t done it yet but I’ll be moving BackgroundOperation() into a class of its own and implementing the method to cancel the operation.

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

Sidebar

Related Questions

I have several classes that I serialize/deserialize, each with a number of properties, some
I have a number Processor classes that will do two very different things, but
I have a python module that defines a number of classes: class A(object): def
I have a number of classes that are decorated with DebuggerDisplayAttribute. I want to
I have a native/unmanaged C++ library with a number of classes that I would
I have a number of classes that are mapped to tables with SQLAlchemy (non-declaratively
I have a number of data classes representing various entities. Which is better: writing
I have a project with a number of different classes querying and modifying data
In Ruby I have often written a number of small classes, and had a
I'm working with a number of 'helper' classes, which affectively have a bunch of

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.