简体   繁体   中英

C# code refactoring Background worker

On .NET Windows form, I have Background worker component that works fine. I have 5 forms, that has basically same Background worker on it with same code. Can I extract this code to other class and somehow use it, considering this is an event? This is code I have on form. It takes 20 lines of code, and it would be nice if this can be refactored. Note: as you can see, I have already put it to other class BackgroundWorkerHelper, but can I also somehow refactor this events on Background worker, so that it is in other class as well, this way code is less and reused.

private void RunBackgroundWorker(string infoLabelText, int imageIndex)
{
    BackgroundWorkerHelper.Run(backgroundWorker, progressBar, infoLabelText, imageIndex);
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorkerHelper.DoWork(backgroundWorker);
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    BackgroundWorkerHelper.ProgressChanged(sender, e, progressBar);
}

private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    BackgroundWorkerHelper.RunWorkerCompleted(sender, e, progressBar);
}

Note: for now I would like to avoid using user control. I know I could do it, but then you have code that handles placing user control and so on. I am still not very good in it.

Here is solution, thanks to rory who gave me idea how to do it. First, I made this class:

public class BackgroundWorkerHelper
{
    private static string _infoLabelText = string.Empty;
    public BackgroundWorker _BackgroundWorker;
    private BarEditItem _marqueeInfo;//this is marquee progress bar

    public BackgroundWorkerHelper(BarEditItem marqueeInfo)
    {
        _marqueeInfo = marqueeInfo;

        _BackgroundWorker = new BackgroundWorker();
        _BackgroundWorker.WorkerReportsProgress = true;
        _BackgroundWorker.WorkerSupportsCancellation = true;

        _BackgroundWorker.DoWork += backgroundWorker_DoWork;
        _BackgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
        _BackgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
    }

    public void Run(string labelText, int imageIndex)
    {
        _marqueeInfo.Caption = labelText;
        _marqueeInfo.ImageIndex = imageIndex;

        if (!_BackgroundWorker.IsBusy)
            _BackgroundWorker.RunWorkerAsync();
        else
            _marqueeInfo.Caption = "Busy processing saving data, please wait...";
    }

    public void DoWork()
    {
        for (int i = 0; i <= 5; i++)
        {
            _BackgroundWorker.ReportProgress(i); // call backgroundWorker_ProgressChanged event and pass i (which is e argument e.ProgressPercentage) to update UI controls
            Thread.Sleep(250);
        }
    }

    public void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        _marqueeInfo.Visibility = BarItemVisibility.Always;
    }

    public void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        _marqueeInfo.Visibility = BarItemVisibility.Never;
    }

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        DoWork();
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        ProgressChanged(sender, e);
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        RunWorkerCompleted(sender, e);
    }

then in FORM, in class level above constructor place

private readonly BackgroundWorkerHelper _backgroundWorkerHelper;

then in Form Constructor instantiate class

_backgroundWorkerHelper = new BackgroundWorkerHelper(marqueeInfo);

and then I just call it in my form

_backgroundWorkerHelper.Run("Saving", 14);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM