倒立的背景工作者

时间:2011-08-02 10:34:20

标签: c# multithreading backgroundworker

我有许多类可以执行操作,通常会逐步执行记录集并为每条记录调用一两个Web服务。

目前这一切都在GUI线程中运行并挂起绘画。首先想到的是使用BackgroundWorker并实现一个很好的进度条,处理错误,完成等。后台工作者启用的所有好东西。

一旦代码点击屏幕,它就开始闻起来。我在每个类中编写了很多后台工作程序,在bw_DoWork方法中重复了大部分ProcessRows方法,并认为应该有更好的方法,而且可能已经完成了。

在我开始重新发明轮子之前,是否存在一个模式或实现,用于分离背景工作者?它需要实现诸如ibackgroundable之类的接口的类,但是这些类仍然可以独立运行,并且只需要很少的更改来实现接口。

编辑:@Henk要求的简化示例:

我有:

    private void buttonUnlockCalls_Click(object sender, EventArgs e)
    {
        UnlockCalls unlockCalls = new UnlockCalls();
        unlockCalls.MaxRowsToProcess = 1000;
        int processedRows = unlockCalls.ProcessRows();
        this.textProcessedRows.text = processedRows.ToString();
    }

我想我想要:

    private void buttonUnlockCalls_Click(object sender, EventArgs e)
    {
        UnlockCalls unlockCalls = new UnlockCalls();
        unlockCalls.MaxRowsToProcess = 1000;

        PushToBackground pushToBackground = new PushToBackground(unlockCalls)
        pushToBackground.GetReturnValue = pushToBackground_GetReturnValue;
        pushToBackground.DoWork();
    }

    private void pushToBackground_GetReturnValue(object sender, EventArgs e)
    {
        int processedRows = e.processedRows;
        this.textProcessedRows.text = processedRows.ToString();
    }

我可以继续这样做,但不想重新发明。

我正在寻找的答案将是“是的,Joe做了一个很好的实现(这里)”或“那是一个代理小部件模式,去读它(这里)”

2 个答案:

答案 0 :(得分:1)

每个操作都需要实现以下界面:

/// <summary>
/// Allows progress to be monitored on a multi step operation
/// </summary>
interface ISteppedOperation
{
    /// <summary>
    /// Move to the next item to be processed.
    /// </summary>
    /// <returns>False if no more items</returns>
    bool MoveNext();

    /// <summary>
    /// Processes the current item
    /// </summary>
    void ProcessCurrent();

    int StepCount { get; }
    int CurrentStep { get; }
}

这分离了处理步骤的枚举。

以下是一个示例操作:

class SampleOperation : ISteppedOperation
{
    private int maxSteps = 100;

    //// The basic way of doing work that I want to monitor
    //public void DoSteppedWork()
    //{
    //    for (int currentStep = 0; currentStep < maxSteps; currentStep++)
    //    {
    //        System.Threading.Thread.Sleep(100);
    //    }
    //}

    // The same thing broken down to implement ISteppedOperation
    private int currentStep = 0; // before the first step
    public bool MoveNext()
    {
        if (currentStep == maxSteps)
            return false;
        else
        {
            currentStep++;
            return true;
        }
    }

    public void ProcessCurrent()
    {
        System.Threading.Thread.Sleep(100);
    }

    public int StepCount
    {
        get { return maxSteps; }
    }

    public int CurrentStep
    {
        get { return currentStep; }
    }

    // Re-implement the original method so it can still be run synchronously
    public void DoSteppedWork()
    {
        while (MoveNext())
            ProcessCurrent();
    }
}

可以从以下形式调用:

private void BackgroundWorkerButton_Click(object sender, EventArgs eventArgs)
{
    var operation = new SampleOperation();

    BackgroundWorkerButton.Enabled = false;

    BackgroundOperation(operation, (s, e) =>
        {
            BackgroundWorkerButton.Enabled = true;
        });
}

private void BackgroundOperation(ISteppedOperation operation, RunWorkerCompletedEventHandler runWorkerCompleted)
{
    var backgroundWorker = new BackgroundWorker();

    backgroundWorker.RunWorkerCompleted += runWorkerCompleted;
    backgroundWorker.WorkerSupportsCancellation = true;
    backgroundWorker.WorkerReportsProgress = true;

    backgroundWorker.DoWork += new DoWorkEventHandler((s, e) =>
    {
        while (operation.MoveNext())
        {
            operation.ProcessCurrent();

            int percentProgress = (100 * operation.CurrentStep) / operation.StepCount;
            backgroundWorker.ReportProgress(percentProgress);

            if (backgroundWorker.CancellationPending) break;
        }
    });

    backgroundWorker.ProgressChanged += new ProgressChangedEventHandler((s, e) =>
    {
        var progressChangedEventArgs = e as ProgressChangedEventArgs;
        this.progressBar1.Value = progressChangedEventArgs.ProgressPercentage;
    });

    backgroundWorker.RunWorkerAsync();
}

我还没有完成它但是我将把BackgroundOperation()移动到它自己的类中并实现取消操作的方法。

答案 1 :(得分:0)

我会将我的非UI代码放入一个新类并使用一个Thread(而不是后台worker)。要显示进度,请将新类触发事件返回到UI并使用Dispatcher.Invoke更新UI。

这里有一些编码,但它更清洁,有效。并且比使用后台工作程序(仅适用于小任务)更易于维护。