namespace System.ComponentModel
{
//
// 摘要:
// 在单独的线程上执行操作。
[DefaultEvent("DoWork")]
[SRDescriptionAttribute("BackgroundWorker_Desc")]
public class BackgroundWorker : Component
{
//
// 摘要:
// 初始化 System.ComponentModel.BackgroundWorker 类的新实例。
public BackgroundWorker();
//
// 摘要:
// 获取一个值,指示应用程序是否已请求取消后台操作。
//
// 返回结果:
// 如果应用程序已请求取消后台操作,则为 true;否则为 false。默认值为 false。
[Browsable(false)]
[SRDescriptionAttribute("BackgroundWorker_CancellationPending")]
public bool CancellationPending { get; }
//
// 摘要:
// 获取一个值,指示 System.ComponentModel.BackgroundWorker 是否正在运行异步操作。
//
// 返回结果:
// 如果 System.ComponentModel.BackgroundWorker 正在运行异步操作,则为 true;否则为 false。
[Browsable(false)]
[SRDescriptionAttribute("BackgroundWorker_IsBusy")]
public bool IsBusy { get; }
//
// 摘要:
// 获取或设置一个值,该值指示 System.ComponentModel.BackgroundWorker 能否报告进度更新。
//
// 返回结果:
// 如果 System.ComponentModel.BackgroundWorker 支持进度更新,则为 true;否则为 false。默认值为 false。
[DefaultValue(false)]
[SRCategoryAttribute("PropertyCategoryAsynchronous")]
[SRDescriptionAttribute("BackgroundWorker_WorkerReportsProgress")]
public bool WorkerReportsProgress { get; set; }
//
// 摘要:
// 获取或设置一个值,该值指示 System.ComponentModel.BackgroundWorker 是否支持异步取消。
//
// 返回结果:
// 如果 System.ComponentModel.BackgroundWorker 支持取消,则为 true;否则为 false。默认值为 false。
[DefaultValue(false)]
[SRCategoryAttribute("PropertyCategoryAsynchronous")]
[SRDescriptionAttribute("BackgroundWorker_WorkerSupportsCancellation")]
public bool WorkerSupportsCancellation { get; set; }
//
// 摘要:
// 调用 System.ComponentModel.BackgroundWorker.RunWorkerAsync 时发生。
[SRCategoryAttribute("PropertyCategoryAsynchronous")]
[SRDescriptionAttribute("BackgroundWorker_DoWork")]
public event DoWorkEventHandler DoWork;
//
// 摘要:
// 调用 System.ComponentModel.BackgroundWorker.ReportProgress(System.Int32) 时发生。
[SRCategoryAttribute("PropertyCategoryAsynchronous")]
[SRDescriptionAttribute("BackgroundWorker_ProgressChanged")]
public event ProgressChangedEventHandler ProgressChanged;
//
// 摘要:
// 当后台操作已完成、被取消或引发异常时发生。
[SRCategoryAttribute("PropertyCategoryAsynchronous")]
[SRDescriptionAttribute("BackgroundWorker_RunWorkerCompleted")]
public event RunWorkerCompletedEventHandler RunWorkerCompleted;
//
// 摘要:
// 请求取消挂起的后台操作。
//
// 异常:
// T:System.InvalidOperationException:
// System.ComponentModel.BackgroundWorker.WorkerSupportsCancellation 为 false。
public void CancelAsync();
//
// 摘要:
// 引发 System.ComponentModel.BackgroundWorker.ProgressChanged 事件。
//
// 参数:
// percentProgress:
// 已完成的后台操作所占的百分比,范围从 0% 到 100%。
//
// 异常:
// T:System.InvalidOperationException:
// System.ComponentModel.BackgroundWorker.WorkerReportsProgress 属性设置为 false。
public void ReportProgress(int percentProgress);
//
// 摘要:
// 引发 System.ComponentModel.BackgroundWorker.ProgressChanged 事件。
//
// 参数:
// percentProgress:
// 已完成的后台操作所占的百分比,范围从 0% 到 100%。
//
// userState:
// 传递到 System.ComponentModel.BackgroundWorker.RunWorkerAsync(System.Object) 的状态对象。
//
// 异常:
// T:System.InvalidOperationException:
// System.ComponentModel.BackgroundWorker.WorkerReportsProgress 属性设置为 false。
public void ReportProgress(int percentProgress, object userState);
//
// 摘要:
// 开始执行后台操作。
//
// 异常:
// T:System.InvalidOperationException:
// System.ComponentModel.BackgroundWorker.IsBusy 为 true。
public void RunWorkerAsync();
//
// 摘要:
// 开始执行后台操作。
//
// 参数:
// argument:
// 要在 System.ComponentModel.BackgroundWorker.DoWork 事件处理程序中执行的后台操作使用的参数。
//
// 异常:
// T:System.InvalidOperationException:
// System.ComponentModel.BackgroundWorker.IsBusy 为 true。
public void RunWorkerAsync(object argument);
//
// 摘要:
// 引发 System.ComponentModel.BackgroundWorker.DoWork 事件。
//
// 参数:
// e:
// 一个 System.EventArgs,其中包含事件数据。
protected virtual void OnDoWork(DoWorkEventArgs e);
//
// 摘要:
// 引发 System.ComponentModel.BackgroundWorker.ProgressChanged 事件。
//
// 参数:
// e:
// 一个 System.EventArgs,其中包含事件数据。
protected virtual void OnProgressChanged(ProgressChangedEventArgs e);
//
// 摘要:
// 引发 System.ComponentModel.BackgroundWorker.RunWorkerCompleted 事件。
//
// 参数:
// e:
// 一个 System.EventArgs,其中包含事件数据。
protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e);
}
}
我总结的backgroundworker一般使用方法:
创建一个backgroundWorker的时候,首先设置属性
WorkerReportsProgress和WorkerSupportsCancellation(不设置的话这个backgroundWorker就不能报告进度和取消线程)
然后去写Dowork函数,里面写你想要在这个线程中完成的工作。
然后在你想开启线程的地方调用 RunWorkerAsync()
在你想取消线程的地方调用CancelAsync(),这时候CancellationPending属性值会被设置为true,然后在Dowork函数中可以加入如果线程被取消应该执行的代码。
在backgroundworker中,dowork里面是不能进行对窗体中控件操作的(会报错,跨线程不安全调用异常),
所以对于窗体控件的操作应该放在progressChanged中,在dowork里面加一句reportProgress来引发progreChanged。