首页 > 代码库 > 多线程BackroundWorker 使用

多线程BackroundWorker 使用

参考文章:http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication4{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //BackgroundWorker 组件用来执行诸如数据库事务、文件下载等耗时的异步操作。        //在应用程序中添加一个BackgroundWorker实例,如果用的是VS,可以从工具上直接拖到应用程序:        BackgroundWorker backgroundWorker1 = new BackgroundWorker();                       //为了开始在后台操作,必须调用BackgroundWorker的RunWorkerAsync()方法,当调用此方时,BackgroundWorker 通过触发DoWork 事件,开始执行后台操作,DoWork 事件的代码是在另一个线程里执行的。        ////也可以利用RunWokerAsync()方法传递参数,backgroundWorker1.RunWorkerAsync(2000/*参数是可选的*/)        private void btnStart_Click(object sender, EventArgs e)        {            //为了显示后台操作的执行进度,首先要使WorkerReportsProgress 等于true,            //然后调用BackgroundWorker的ReportProgress()方法,通过它传递操作完成的进度值,            //此外,该方法触发ProgressChanged事件,在是此事件中,通过ProgressChangedEventArgs的实例,            //接收到主线程传递过来的参数。            backgroundWorker1.WorkerReportsProgress= true;            //为了使 BackgroundWorker 可以取消后台正在执行的操作,            //首先要把属性WorkerSupportsCancellation 的值设置为 true。            //接着调用CancelAsync()方法,该方法使得属性CancellationPending 为true,            //利用CancellationPending 属性,可以判断是否取消后台异步操作。            backgroundWorker1.WorkerSupportsCancellation = true;            backgroundWorker1.RunWorkerAsync(1000);            backgroundWorker1.DoWork+=new DoWorkEventHandler(backgroundWorker1_DoWork);            backgroundWorker1.ProgressChanged+=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);            backgroundWorker1.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);        }        private void btnCancel_Click(object sender, EventArgs e)        {            backgroundWorker1.CancelAsync();        }        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)        {            //允许长时间的操作             int input = (int)e.Argument;                        for (int i = 0; i < 11;i++ )            {                System.Threading.Thread.Sleep(input);                backgroundWorker1.ReportProgress(i * 10);                if (backgroundWorker1.CancellationPending)                {                    e.Cancel = true;                    return;                }            }            //在执行DoWork 事件时,DoWorkEventArgs 实例的Result 属性,            //返回值到用户;在RunWorkerCompleted 事件里,            //RunWorkerCompletedEventArgs 实例的Result 属性接收值            e.Result = 10;                }        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)        {            //progressBar1是ProgressBar控件            progressBar1.Value =http://www.mamicode.com/ e.ProgressPercentage;                 }       // 当后台操作完成以后,无论是completed 还是cancelled,则RunWorkerCompleted 事件被触发,        //通过此方法可以将后台操作的完成结果反馈给用户;另外,通过RunWorkerCompletedEventArgs        //实例的Cancelled 属性,以判断是否是Cancel操作使得后台操作终止;        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)        {            if (e.Cancelled)            {                MessageBox.Show("Operation cancelled");            }            else            {                MessageBox.Show("Operation completed");            }            //在此处接收backgroundWorker1_DoWork传递回来的值            int returnValue = http://www.mamicode.com/(int)e.Result;            label1.Text = returnValue.ToString();                }               }}

 

多线程BackroundWorker 使用