Search code examples
c#visual-studio-2015backgroundworker

Access background worker from a different class


I'm separating out logic from one class to another in my Windows Form application. When all in one class, it works fine but I am moving the logic to it's own respective classes now. Having some issues getting at the backgroundWorker from a different class.

I have in class A

public partial class ClassA : Form
    BackgroundWorker essentialBgWorker = new BackgroundWorker();

    public ClassA()
        {
            InitializeComponent();
            //essentialBgWorker
            essentialBgWorker.DoWork += new DoWorkEventHandler(essentialBgWorker_DoWork);
            essentialBgWorker.ProgressChanged += new ProgressChangedEventHandler(essentialBgWorker_ProgressChanged);
            essentialBgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(essentialBgWorker_RunWorkerCompleted);
            essentialBgWorker.WorkerReportsProgress = true;
        }

That is a form that has a button which when clicked, copies files to another directory.

private void copyButton_Click(object sender, EventArgs e)
{
    clickedButton = ((Button)sender).Name.ToString();
    itemsChanged = ((Button)sender).Text.ToString();
    essentialBgWorker.RunWorkerAsync();
}

This runs the background worker:

public void essentialBgWorker_DoWork(object sender, DoWorkEventArgs e)
{
    string buttonSender = clickedButton; //copyButton, deleteButton, etc.
    switch (buttonSender)
    {
        case "copyButton":
            //this is pseudocode - the important part being that this is where I would call the method from the other class
            ClassB.copyDocuments();
            break;
        case "deleteButton":
            //this is pseudocode - the important part being that this is where I would call the method from the other class
            ClassB.deleteDocuments();
            break;
        default:
            essentialBgWorker.CancelAsync();
            break;
    }
}

In my other class (ClassB) for this example I have the method that is called when the button is clicked (should be handling the logic).

public void copyDocuments()
{
    ClassA classA = new ClassA();
    //
    //the logic that handles copying the files
    //gets the filecount!

    //Report to the background worker
    int totalFileCount = fileCount;
    int total = totalFileCount; //total things being transferred
    for (int i = 0; i <= total; i++) //report those numbers
    {
        System.Threading.Thread.Sleep(100);
        int percents = (i * 100) / total;
        classA.essentialBgWorker.ReportProgress(percents, i);

        //2 arguments:
        //1. procenteges (from 0 t0 100) - i do a calcumation 
        //2. some current value!
    }
    //Do the copying here
}

I keep having an issue with this line:

classA.essentialBgWorker.ReportProgress(percents, i);

This line worked when it was in ClassA - but once bringing it into ClassB gives an error:

ClassA.essentialBgWorker' is inaccessible due to it's protection level

Just looking for some assistance on the proper way to get it to fire from the other class.


Solution

  • You must add public modifier to essentialBgWorker

    public BackgroundWorker essentialBgWorker = new BackgroundWorker();