Search code examples
c#.netbackgroundworker

Designing an Interface for BackgroundWorker


In my windows forms applications I have a class that extends a backgroundworker, let's call it ExtendedBGW1.cs in my form class I declare it like a member variable so I have scope for the entire class like so:

public partial class Main : Form
{
    ExtendedBGW1 ebgw1;
}

Later on in the forms constructor I do this

public Main()
{
    InitializeComponent();

    ebgw1 = new ExtendedBGW1();

    InitializeBackgoundWorker();
}

My InitializeBackgroundWoker() method looks like this

private void InitializeBackgoundWorker()
{
    ebgw1.DoWork += new DoWorkEventHandler(ebgw1.worker_DoWork);
    ebgw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(processWorkerCompleted);
    ebgw1.ProgressChanged += new ProgressChangedEventHandler(processProgressChanged);
    ebgw1.WorkerReportsProgress = true;
    ebgw1.WorkerSupportsCancellation = true;
}

Now comes my design problem. I know now that I am going to have different more classes like my extenededBGW1.cs that will extend BackGroundWorker so I was thinking that if I create a IExtenedBackGroundWorker I could do something like this.

public partial class Main : Form
{
    IExtenedBackGroundWorker ebgw1;
}

And still have the proper scope for the Main class. Then I could just create whichever implementation of IExtendedBackGroundWorker I need later on.

I can make the interface for the methods and properties without much issue but am really running into a problem when I try to wire up the events correctly between the interface the base class and the Main class.

Does anyone have any ideas?

Here are the errors I get in the Main

Error   1   Cannot assign to 'DoWork' because it is a 'method group'

and here is the error I get in my implemetation of the interface

Error   5   The event 'System.ComponentModel.BackgroundWorker.DoWork' can only appear on the left hand side of += or -= 

here is what my interface looks like right now:

interface IExtendedBackGroundWorker 
{
    bool IsBusy { get; }

    bool WorkerReportsProgress { get; set; }

    bool WorkerSupportsCancellation { get; set; }

    List<CompareObject> ObjList { get; set; }

    string FilePath { get; set; }

    void RunWorkerAsync();

    void CancelAsync();

    void DoWork();

    void worker_DoWork(object sender, DoWorkEventArgs e);

    void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e);

    void ProgressChanged(object sender, ProgressChangedEventArgs e);
}

Solution

  • Easy, just 1 - 2 - 3 - Done

    public interface IMyWorker
    {
        bool WorkerReportsProgress { get; set; }
        bool WorkerSupportsCancellation { get; set; }
        event DoWorkEventHandler DoWork;
        event ProgressChangedEventHandler ProgressChanged;
        event RunWorkerCompletedEventHandler RunWorkerCompleted;
    }
    
    public class MyWorker : BackgroundWorker, IMyWorker
    {
    
    }
    

    Usage:

    namespace stackOverflow
    {
    class Program
    {
        static void Main(string[] args)
        {
            IMyWorker worker = new MyWorker();
            worker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
        }
    
        static void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            throw new NotImplementedException();
        }
      }
    }
    

    Have fun :)