C#代码重构后台工作者

时间:2018-07-03 14:48:48

标签: c# refactoring

在.NET Windows窗体上,我有可以正常工作的Background worker组件。我有5种形式,具有与相同代码的基本背景工作人员相同。 考虑到这是事件,我是否可以将此代码提取到其他类并以某种方式使用它?这是我在表单上的代码。它需要20行代码,并且如果可以重构这将是很好的。注意:如您所见,我已经将它放在其他类BackgroundWorkerHelper中,但是我还可以以某种方式在Background worker上重构此事件,以便它也可以在其他类中进行重构,这样可以减少代码并重用。

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);
}

注意:目前,我想避免使用用户控件。我知道我可以做到,但是然后您有了处理放置用户控件等的代码。我还是不太好。

1 个答案:

答案 0 :(得分:0)

这是解决方案,这要感谢rory给我的想法。首先,我参加了这堂课:

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);
    }

然后在FORM中,位于构造函数上方的类级别

private readonly BackgroundWorkerHelper _backgroundWorkerHelper;

然后在Form Constructor实例化类

_backgroundWorkerHelper = new BackgroundWorkerHelper(marqueeInfo);

然后我以表单的形式调用它

_backgroundWorkerHelper.Run("Saving", 14);