Search code examples
c#backgroundworker

Background worker class and passing messages using progress events from a different class in c#


So i have one class which starts a new class in a new background worker, and the background worker passes status messages back using the progresschanged section.

When i try and and use this by typing

classname.Dataworker.reportprogress(5)

from a seperate class i get an error that I am using an object before definition.

The examples I have found all use a single class and different functions within that.

It may be a stupid easy mistake but i just can't see it, thanks for any help you can give!

A general overview of my code is:

//form class

public static BackgroundWorker bw = new BackgroundWorker();

onbuttonclick
{
        installer install = new installer();
        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += class2.aFunction;
        bw.ProgressChanged += new ProgressChangedEventHandler(mainForm_InstallerEvent);
        bw.RunWorkerAsync();
}

private void mainForm_InstallerEvent(object sender, ProgressChangedEventArgs e)
{

        lbl.Text = e.UserState.ToString();
}

////class2 the background worker class

aFunction
{
        InstallerForm.bw.ReportProgress(5); //errors on this!
}

Solution

  • Thanks for the help, between those answers and one I found I've managed to get it work, the line I was missing is: BackgroundWorker worker = (BackgroundWorker)sender;

    and then reference that worker object with worker.reportprogress(..)

    The guide i found useful is: http://www.nerdparadise.com/tech/coding/csharp/backgroundworker/

    perfect, thanks guys :)