名称 说明
【20091229-01】BackgroundWorker控件用法 CancelAsync 请求取消挂起的后台操作。
【20091229-01】BackgroundWorker控件用法 CreateObjRef  创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (从 MarshalByRefObject 继承。)
【20091229-01】BackgroundWorker控件用法 Dispose  已重载。 释放由 Component 继承。)
【20091229-01】BackgroundWorker控件用法 Equals  已重载。 确定两个 Object 继承。)
【20091229-01】BackgroundWorker控件用法 GetHashCode  用作特定类型的哈希函数。Object 继承。)
【20091229-01】BackgroundWorker控件用法 GetLifetimeService  检索控制此实例的生存期策略的当前生存期服务对象。 (从 MarshalByRefObject 继承。)
【20091229-01】BackgroundWorker控件用法 GetType  获取当前实例的 Object 继承。)
【20091229-01】BackgroundWorker控件用法 InitializeLifetimeService  获取控制此实例的生存期策略的生存期服务对象。 (从 MarshalByRefObject 继承。)
【20091229-01】BackgroundWorker控件用法【20091229-01】BackgroundWorker控件用法 ReferenceEquals  确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
【20091229-01】BackgroundWorker控件用法 ReportProgress 已重载。 引发 ProgressChanged 事件。
【20091229-01】BackgroundWorker控件用法 RunWorkerAsync 已重载。 开始执行后台操作。
【20091229-01】BackgroundWorker控件用法 ToString  返回包含 Component 的名称的 Component 继承。)

 

(请参见 受保护的属性

  名称 说明
【20091229-01】BackgroundWorker控件用法 CancellationPending 获取一个值,指示应用程序是否已请求取消后台操作。
【20091229-01】BackgroundWorker控件用法 Container  获取 Component 继承。)
【20091229-01】BackgroundWorker控件用法 IsBusy 获取一个值,指示 BackgroundWorker 是否正在运行异步操作。
【20091229-01】BackgroundWorker控件用法 Site  获取或设置 ComponentComponent 继承。)
【20091229-01】BackgroundWorker控件用法 WorkerReportsProgress 获取或设置一个值,该值指示 BackgroundWorker 能否报告进度更新。
【20091229-01】BackgroundWorker控件用法 WorkerSupportsCancellation 获取或设置一个值,该值指示 BackgroundWorker 是否支持异步取消。

 

 

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace BackgroundWork
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
            
this.backgroundWorker.WorkerReportsProgress = true;
            
this.backgroundWorker.WorkerSupportsCancellation = true;
        }
        
/// <summary>
        
/// 处理耗时操作,不能与UI交互(不能出现UI控件)
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            
for (int i = 0; i < 1001; i++)
            {
                
//--backgroundWorker.CancelAsync方法请求取消后台操作后,
                
//--backgroundWorker.CancelAsync属性为True
                if (this.backgroundWorker.CancellationPending)
                {
                    e.Cancel 
= true;//取消事件
                    break;
                }
                Thread.Sleep(
10);
                
this.backgroundWorker.ReportProgress(i,"处理中");
                
//e.Result = i ;
            }
        }

        
/// <summary>
        
/// 耗时操作过程中与UI交互,比如显示进度
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
this.progressBar1.Value = e.ProgressPercentage;
          
            
int per=(this.progressBar1.Value/10);

            
this.label1.Text = per.ToString()+"%";
            
this.Text = e.UserState.ToString();
            
        }

        
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            
if (e.Cancelled)
            {
                MessageBox.Show(
"操作被取消");
                
this.progressBar1.Value = 0;
                
this.label1.Text = "0%";
            }
            
else
            {
                MessageBox.Show(
"完成");
                
this.Text = "完成";
            }
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
            
this.backgroundWorker.RunWorkerAsync();

        }

        
private void button2_Click(object sender, EventArgs e)
        {
            
this.backgroundWorker.CancelAsync();
        }
    }
}

 

 

相关文章: