Click here to Skip to main content
14,454,989 members

A Wrapper around BackgroundWorker

Rate this:
3.71 (11 votes)
Please Sign up or sign in to vote.
3.71 (11 votes)
17 Feb 2020CPOL
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.

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

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:

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)

Share

About the Author

ugo.marchesini
Software Developer (Senior)
Italy Italy
No Biography provided

Comments and Discussions

 
QuestionBackgroundWorker Cancellation Pin
george bolovan20-Feb-20 9:49
professionalgeorge bolovan20-Feb-20 9:49 
Questiona question on limits you describe for this code Pin
BillWoodruff18-Feb-20 22:40
mveBillWoodruff18-Feb-20 22:40 
QuestionIncorrect usage of Dispose(), massive memory leaks Pin
Stacy Dudovitz18-Feb-20 11:42
professionalStacy Dudovitz18-Feb-20 11:42 
AnswerRe: Incorrect usage of Dispose(), massive memory leaks Pin
Member 1045006018-Feb-20 11:51
MemberMember 1045006018-Feb-20 11:51 
QuestionWhy not use async/await? Pin
gr8gonzo18-Feb-20 11:19
Membergr8gonzo18-Feb-20 11:19 
AnswerRe: Why not use async/await? Pin
Stacy Dudovitz18-Feb-20 11:52
professionalStacy Dudovitz18-Feb-20 11:52 
QuestionWeird coding standards Pin
ManselD18-Feb-20 8:48
MemberManselD18-Feb-20 8:48 
QuestionI see a massive flaw here PinPopular
Pete O'Hanlon16-Feb-20 5:30
communityengineerPete O'Hanlon16-Feb-20 5:30 
AnswerRe: I see a massive flaw here Pin
ugo.marchesini17-Feb-20 2:36
professionalugo.marchesini17-Feb-20 2:36 
GeneralRe: I see a massive flaw here Pin
Jean-Pierre Bachmann18-Feb-20 3:20
professionalJean-Pierre Bachmann18-Feb-20 3:20 
GeneralRe: I see a massive flaw here Pin
BillWoodruff18-Feb-20 22:36
mveBillWoodruff18-Feb-20 22:36 
GeneralRe: I see a massive flaw here Pin
Jean-Pierre Bachmann23-Feb-20 9:38
professionalJean-Pierre Bachmann23-Feb-20 9:38 

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.

Tip/Trick
Posted 14 Feb 2020

Tagged as

Stats

16.9K views
12 bookmarked