在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示,必要时还要控制后台线程中断当前操作。

以前,类似的应用会比较麻烦,需要写的代码较多,也很容易出现异常。在.net中,提供了一个组件BackgroundWorker就是专门解决这个问题的。BackgroundWorker类允许在单独的专用线程上运行操作。 耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面(UI)似乎处于停止响应状态。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题。

使用这个组件其实非常简单,例如,我们做一个类似如下界面的进度条的小例子,在后台线程中进行耗时运算,同时刷新界面上的进度条。
过程如下:
1.新建一个windows窗体应用程序,如:BackgroundWorkerProgressBarDemo
2.拖一个ProgressBar(progressBar1)和一个BackgroundWorker (backgroundWorker1)到Form上。
3.把下面的代码copy过去就ok了,代码注释的很详细,可以按照需要修改。

复制代码
namespace BackgroundWorkerProgressBarDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Shown += new EventHandler(Form1_Shown);

            // To report progress from the background worker we need to set this property
            backgroundWorker1.WorkerReportsProgress = true;

            // This event will be raised on the worker thread when the worker starts
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

            // This event will be raised when we call ReportProgress
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }

        void Form1_Shown(object sender, EventArgs e)
        {
            // Start the background worker
            backgroundWorker1.RunWorkerAsync();
        }

        // On worker thread so do our thing!
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            // Your background task goes here
            for (int i = 0; i <= 100; i++)
            {
                // Report progress to 'UI' thread
                backgroundWorker1.ReportProgress(i);
                // Simulate long task
                System.Threading.Thread.Sleep(100);
            }
        }

        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
        }
    }
}
复制代码

若要为后台操作做好准备,请添加DoWork事件的事件处理程序,在此事件处理程序中调用耗时的操作。

若要开始此操作,请调用RunWorkerAsync。

若要收到进度更新的通知,请处理ProgressChanged 事件。

若要在操作完成时收到通知,请处理RunWorkerCompleted 事件。

注意:

您必须非常小心,确保在 DoWork 事件处理程序中不操作任何用户界面对象。 而应该通过 ProgressChanged 和 RunWorkerCompleted 事件与用户界面进行通信。

BackgroundWorker 事件不跨 AppDomain 边界进行封送处理。 请不要使用 BackgroundWorker 组件在多个 AppDomain 中执行多线程操作。

如果后台操作需要参数,请在调用 RunWorkerAsync 时给出参数。 在 DoWork 事件处理程序内部,可以从 DoWorkEventArgs.Argument 属性中提取该参数。

参考:

http://msdn.microsoft.com/zh-cn/library/vstudio/System.ComponentModel.BackgroundWorker(v=vs.100).aspx

Demo2 取消,暂停,恢复

public partial class BackgroundWorker111 : Window
{
private BackgroundWorker backgroundWorker1 = new BackgroundWorker();
private static ManualResetEvent _eventWorkList = new ManualResetEvent(false);
private bool IsWait = false;
public BackgroundWorker111()
{
InitializeComponent();
// To report progress from the background worker we need to set this property
backgroundWorker1.WorkerReportsProgress = true;

backgroundWorker1.WorkerSupportsCancellation = true;

// This event will be raised on the worker thread when the worker starts
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

// This event will be raised when we call ReportProgress
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}


void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Your background task goes here
for (int i = 0; i <= 100; i++)
{
if (backgroundWorker1.CancellationPending) //这里判断一下是否用户要求取消后台进行,并可以尽早退出。
{
backgroundWorker1.ReportProgress(i, String.Format("当前值是 {0},操作被用户申请中断", i));
return;
}
if(IsWait)
_eventWorkList.WaitOne();
// Report progress to 'UI' thread
backgroundWorker1.ReportProgress(i);
// Simulate long task
System.Threading.Thread.Sleep(100);
}
}

// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar1.Value = e.ProgressPercentage;


this.progressBar1.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("运算终于完成了");
}

private void button_Click(object sender, RoutedEventArgs e)
{

if (IsWait)
_eventWorkList.Set();
if (this.backgroundWorker1.IsBusy)
return;
this.backgroundWorker1.RunWorkerAsync();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
backgroundWorker1.CancelAsync();
}

private void button2_Click(object sender, RoutedEventArgs e)
{
IsWait = true;
_eventWorkList.Reset();
}
}