C# BackgroundWorker分析以及使用

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。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值