Click here to Skip to main content
15,867,915 members
Articles / Programming Languages / C#
Tip/Trick

A Wrapper around BackgroundWorker

Rate me:
Please Sign up or sign in to vote.
3.71/5 (11 votes)
17 Feb 2020CPOL 27.4K   13   16
An alternative way to use BackgroundWorker: more readable, more concise
There are many ways to use BackgroundWorker: lambdas, anonymous delegate and events. All have pros and cons but I find that they are not so readable. Here, you will see an alternative way to use BackgroundWorker.

Introduction

The objective of this tip is to show an alternative way to use BackgroundWorker.

Background

Microsoft documentation shows how to use BackgroundWorker with event and in these examples, it's possible to use other methods.

C#
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(
    () =>
    {
       // Do Something
    }
);
bgw.DoWork += (sender, e) => { ... }

bgw.DoWork += delegate { ... }

I find these ways not so readable, so I suggest the method in the next chapter.

Using the Code

C#
public class PatientMan
    {
        private Action _DoWork = null;
        private Boolean _IsDoWorkSubscribe = false;
        private DoWorkEventHandler _DoWorkHandler;

        private Action _RunWorkerCompleted = null;
        private Boolean _IsRunWorkerCompletedSubscribe = false;
        private RunWorkerCompletedEventHandler _RunWorkerCompletedHandler;

        private Action<int> _ProgressChanged = null;
        private Boolean _IsProgressChangedSubscribe = false;
        private ProgressChangedEventHandler _ProgressChangedHandler;

        private BackgroundWorker _Worker = new BackgroundWorker();

        public PatientMan()
        {

        }

        public PatientMan(Action doWorkAction,
                            Action runWorkerCompleted)
        {
            this.SubcribeDoWork(doWorkAction);
            this.SubcribeRunWorkerCompleted(runWorkerCompleted);
        }

        private void SubcribeDoWork(Action doWorkAction)
        {
            this._DoWork = doWorkAction;
            this._DoWorkHandler = delegate { this._DoWork(); };
            this._Worker.DoWork += _DoWorkHandler;
            this._IsDoWorkSubscribe = true;
        }

        private void SubcribeRunWorkerCompleted(Action runWorkerCompleted)
        {
            this._RunWorkerCompleted = runWorkerCompleted;
            this._RunWorkerCompletedHandler
                =
            delegate {
                this._RunWorkerCompleted();
                this.UnsubscribeEvents();
            };
            this._Worker.RunWorkerCompleted += this._RunWorkerCompletedHandler;
            this._IsRunWorkerCompletedSubscribe = true;
        }

        private void SubcribeProgressChanged(Action<int> progressChanged)
        {
            this._ProgressChanged = progressChanged;
            this._ProgressChangedHandler = (obj, ev) =>
            {
                this._ProgressChanged(ev.ProgressPercentage);
            };
            this._Worker.ProgressChanged += this._ProgressChangedHandler;
            this._Worker.WorkerReportsProgress = true;
            this._IsProgressChangedSubscribe = true;
        }


        public void RunWorkerAsync()
        {
            this._Worker.RunWorkerAsync();
        }


        public  void ReportProgress(int percentage)
        {
            if(this._ProgressChanged != null)
            {
                if (this._IsProgressChangedSubscribe == true)
                {
                    this._Worker.ReportProgress(percentage);
                }
                else
                {
                    throw new ArgumentNullException();
                }
            }
            else
            {
                throw new NullReferenceException();
            }
        }

        private void UnsubscribeEvents()
        {
            if (this._IsDoWorkSubscribe == true)
            {
                this._Worker.DoWork -= this._DoWorkHandler;
                this._IsDoWorkSubscribe = false;
            }

            if (this._IsRunWorkerCompletedSubscribe == true)
            {
                this._Worker.RunWorkerCompleted -= this._RunWorkerCompletedHandler;
                this._IsRunWorkerCompletedSubscribe = false;
            }

            if (this._IsProgressChangedSubscribe == true)
            {
                this._Worker.ProgressChanged -=  this._ProgressChangedHandler;
                this._Worker.WorkerReportsProgress = false;
                this._IsProgressChangedSubscribe = false;
            }
        }

        public void Dispose()
        {
            this.UnsubscribeEvents();
            this._Worker.Dispose();
        }

        public void SetEvents(Action doWorkAction,
                            Action runWorkerCompleted,
                            Action<int> progressChanged,
                            Boolean unsubscribeEvents = false)
        {
            this.UnsubscribeEvents();

            this.SubcribeDoWork(doWorkAction);
            this.SubcribeRunWorkerCompleted(runWorkerCompleted);
            this.SubcribeProgressChanged(progressChanged);
        }
    }

PatientMan class provides basic features wrapped around BackgroundWorker.

It's possible to use it in this way:

C++
public class SandBoxPatientMan
    {
        public SandBoxPatientMan(int algo)
        {

            switch(algo)
            {
                case 0:
                    {
                        Action doWork = delegate
                        {
                            // Do something long
                            for (int i = 0; i < 10; i++)
                            {
                                System.Threading.Thread.Sleep(1000);
                            }
                        };

                        Action runWorkerCompleted = delegate
                        {
                            // Do something after you did something long

                            Console.WriteLine("runWorkerCompleted " + algo);
                        };

                        PatientMan patientMan = new PatientMan(doWork, runWorkerCompleted);

                        patientMan.RunWorkerAsync();
                    }
                    break;

                case 1:
                    {
                        PatientMan patientMan = new PatientMan();

                        Action doWork = delegate
                        {
                            // Do something long

                            for (int i = 0; i < 10; i++)
                            {
                                System.Threading.Thread.Sleep(1000);

                                patientMan.ReportProgress(i);
                            }
                        };

                        Action runWorkerCompleted = delegate
                        {
                            // Do something after you did something long

                            Console.WriteLine("runWorkerCompleted " + algo);
                        };

                        Action<int> progressChanged = (percentage) =>
                        {
                            // Report a progress during a long process

                            Console.WriteLine("progressChanged " + percentage);
                        };

                        patientMan.SetEvents(doWork, runWorkerCompleted, progressChanged);

                        patientMan.RunWorkerAsync();
                    }
                    break;
            }



        }
    }

Passing delegates via parameter is more readable and concise.

In my experience, it is also easier to debug.

Points of Interest

This class can't replace complete BackgroundWorker, it needs some updates like Cancellation or IsBusy.

I use it often and I hope the reader finds it useful. 

History

In version 2 I updated the code after a suggestion in the comments about memory leaks.

  • 14th February, 2020: Version 1
  • 17th February, 2020: Version 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVB.net .net core sandbox implementation Pin
Padanian19-Mar-20 4:58
Padanian19-Mar-20 4:58 
QuestionBackgroundWorker Cancellation Pin
george bolovan20-Feb-20 8:49
professionalgeorge bolovan20-Feb-20 8:49 
Questiona question on limits you describe for this code Pin
BillWoodruff18-Feb-20 21:40
professionalBillWoodruff18-Feb-20 21:40 
QuestionIncorrect usage of Dispose(), massive memory leaks Pin
Stacy Dudovitz18-Feb-20 10:42
professionalStacy Dudovitz18-Feb-20 10:42 
AnswerRe: Incorrect usage of Dispose(), massive memory leaks Pin
Member 1045006018-Feb-20 10:51
Member 1045006018-Feb-20 10:51 
QuestionWhy not use async/await? Pin
gr8gonzo18-Feb-20 10:19
gr8gonzo18-Feb-20 10:19 
AnswerRe: Why not use async/await? Pin
Stacy Dudovitz18-Feb-20 10:52
professionalStacy Dudovitz18-Feb-20 10:52 
QuestionWeird coding standards Pin
ManselD18-Feb-20 7:48
ManselD18-Feb-20 7:48 
QuestionI see a massive flaw here Pin
Pete O'Hanlon16-Feb-20 4:30
subeditorPete O'Hanlon16-Feb-20 4:30 
AnswerRe: I see a massive flaw here Pin
ugo.marchesini17-Feb-20 1:36
professionalugo.marchesini17-Feb-20 1:36 
GeneralRe: I see a massive flaw here Pin
GerVenson18-Feb-20 2:20
professionalGerVenson18-Feb-20 2:20 
GeneralRe: I see a massive flaw here Pin
BillWoodruff18-Feb-20 21:36
professionalBillWoodruff18-Feb-20 21:36 
GeneralRe: I see a massive flaw here Pin
GerVenson23-Feb-20 8:38
professionalGerVenson23-Feb-20 8:38 
GeneralRe: I see a massive flaw here Pin
BillWoodruff25-Feb-20 16:24
professionalBillWoodruff25-Feb-20 16:24 
that is not an example relevant to the context of this discussion; it does not explain the reasons for your assertions.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

GeneralRe: I see a massive flaw here Pin
GerVenson25-Feb-20 23:51
professionalGerVenson25-Feb-20 23:51 
GeneralRe: I see a massive flaw here Pin
BillWoodruff29-Feb-20 20:23
professionalBillWoodruff29-Feb-20 20:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.