C#进度条(Backgroudworker)

  C#进度条(Backgroudworker)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace 进度条2

{

    public partial class Form1 : Form

    {

        private BackgroundWorker worker = new BackgroundWorker();

        int N = 0;

        public Form1()

        {

            InitializeComponent();

            worker.WorkerReportsProgress = true;

            worker.WorkerSupportsCancellation = true;

            worker.DoWork += new DoWorkEventHandler(DoWork);                               

            worker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged);     

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork); 

        }

        public void DoWork(object sender, DoWorkEventArgs e)

        {

            e.Result = ComputeFibonacci(worker, e);

        }

        public void ProgessChanged(object sender, ProgressChangedEventArgs e)

        {

            progressBarX1.Value = e.ProgressPercentage;

            int V =(int)( e.ProgressPercentage / N);

            progressBarX1.Text = Convert.ToString(V) + "%";           

        }

        public void CompleteWork(object sender, RunWorkerCompletedEventArgs e)

        {

            progressBarX1.Text = "处理完毕!";

        }

        private int ComputeFibonacci(object sender, DoWorkEventArgs e)

        {

            for (int i = 0; i <= 92800; i++)

            {

                if (worker.CancellationPending)

                {

                    e.Cancel = true;

                    return -1;

                }

                else

                {

                    worker.ReportProgress(i);       //引发ProgessChanged事件

                }

            }

            return -1;

        }

        private void btnStart_Click(object sender, EventArgs e)

        {

            N = 92800 / 100;

            progressBarX1.Maximum = 92800;

            worker.RunWorkerAsync();        //开始执行后台操作

        }

        private void btnPause_Click(object sender, EventArgs e)

        {

            worker.CancelAsync();           //请求暂停后台操作

        }

     }

}

转载于:https://www.cnblogs.com/kisstome88/archive/2009/05/31/1492642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值