【20091229-01】BackgroundWorker控件用法

  名称 说明
Public method CancelAsync 请求取消挂起的后台操作。
Public method CreateObjRef  创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (从 MarshalByRefObject 继承。)
Public method Dispose  已重载。 释放由 Component 占用的资源。 (从 Component 继承。)
Public method Equals  已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
Public method GetHashCode  用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)
Public method GetLifetimeService  检索控制此实例的生存期策略的当前生存期服务对象。 (从 MarshalByRefObject 继承。)
Public method GetType  获取当前实例的 Type。 (从 Object 继承。)
Public method InitializeLifetimeService  获取控制此实例的生存期策略的生存期服务对象。 (从 MarshalByRefObject 继承。)
Public method Static ReferenceEquals  确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
Public method ReportProgress 已重载。 引发 ProgressChanged 事件。
Public method RunWorkerAsync 已重载。 开始执行后台操作。
Public method ToString  返回包含 Component 的名称的 String(如果有)。不应重写此方法。 (从 Component 继承。)

 

(请参见 受保护的属性

  名称 说明
Public property CancellationPending 获取一个值,指示应用程序是否已请求取消后台操作。
Public property Container  获取 IContainer,它包含 Component。 (从 Component 继承。)
Public property IsBusy 获取一个值,指示 BackgroundWorker 是否正在运行异步操作。
Public property Site  获取或设置 ComponentISite。 (从 Component 继承。)
Public property WorkerReportsProgress 获取或设置一个值,该值指示 BackgroundWorker 能否报告进度更新。
Public property 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();
        }
    }
}

 

 

posted @ 2009-12-29 01:19  WillWayer  阅读(371)  评论(0编辑  收藏  举报