Search code examples
c#.netwinformsbackgroundworker

BackgroundWorker report progress from external Class?


I have a working solution that reports progress & text to a progress bar and a label on the applications's main form. I have now moved my worker methods to a class to they are accessible across multiple forms etc.

Within the worker methods are BW.ReportProgress() statements that push back the progress and text to the BackgroundWorker in the main form.

To give a better idea here is the file layout:

MainScreen.cs

List repSelected = new List();
XMLandRar xXMLandRar = new XMLandRar();

private void Rarbtn_Click(object sender, EventArgs e)
        {
            GetReps();

            //Run worker
            if (!CreateRarBW.IsBusy)
            {
                CreateRarBW.RunWorkerAsync();
            }
        }

//Worker
private void CreateRarBW_DoWork(object sender, DoWorkEventArgs e)
{
    xXMLandRar.RarFiles(repSelected);
}

//Progress reporting
private void CreateRarBW_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progBar.Value = e.ProgressPercentage;
    Statuslbl.Text = e.UserState.ToString();
}

Then my newly created Class that encompasses all of the worker methods and is to push progress to the main form.

XMLandRar.cs

public class XMLandRar
{
    public void RarFiles(List repSelected)
    {
        int step = 100 / repSelected.Count();
        int i = 0;
        //Iterate through list and run rar for each
        foreach (string rep in repSelected)
        {
            CreateRarBW.ReportProgress(i, "Raring files for " + rep);
            DirectoryExists(rep);
            ProcessRunner(rep);
            i += step;
            CreateRarBW.ReportProgress(i, "Raring files for " + rep);
        }
    }
}

The problem I am having is that in the XMLandRar class the CreateRarBW is not recognised (obviously) - how can I make a ReportProgress call to the BW in the main screen of the application?


Solution

  • Create an event in your XMLandRar class which you could subscribe to.

    This way the XMLandRar class doesn't need to know or care about the UI or progressbar, it only cares about sending a message if anyone would listen. And there can also be more than one subscriber (let's say if you want to report to the background worker and a log, maybe)

    Example:

    private void Rarbtn_Click(object sender, EventArgs e)
    {
        GetReps();
    
        //Run worker
        if (!CreateRarBW.IsBusy)
        {
            // This should be done once, maybe in the contructor. Bind to new event.
            xXMLandRar.ReportProgress += new EventHandler<XMLandRar.ProgressArgs>(xXMLandRar_ReportProgress);
    
            CreateRarBW.RunWorkerAsync();
        }
    }
    
    protected void xXMLandRar_ReportProgress(object sender, XMLandRar.ProgressArgs e)
    {
        // Call the UI backgroundworker
        CreateRarBW.ReportProgress(e.Percentage, e.Message);
    }
    
    public class XMLandRar
    {
        // Event handler to bind to for reporting progress
        public EventHandler<ProgressArgs> ReportProgress;
    
        // Eventargs to contain information to send to the subscriber
        public class ProgressArgs : EventArgs
        {
            public int Percentage { get; set; }
            public string Message { get; set; }
        }
    
        public void RarFiles(List repSelected)
        {
            int step = 100 / repSelected.Count();
            int i = 0;
            //Iterate through list and run rar for each
            foreach (string rep in repSelected)
            {
                // Report progress if somebody is listening (subscribed)
                if (ReportProgress != null)
                {
                    ReportProgress(this, new ProgressArgs { Percentage = i, Message = "Raring files for " + rep });
                }
    
                DirectoryExists(rep);
                ProcessRunner(rep);
                i += step;
    
                // Report progress if somebody is listening (subscribed)
                if (ReportProgress != null)
                {
                    ReportProgress(this, new ProgressArgs { Percentage = i, Message = "Raring files for " + rep });
                }
            }
        }
    }