C#进度条实现实例是如何操作的呢?让我们看看下面的代码:
- using System;
- using System.Collections.Generic;
- //C#进度条实现实例
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace 进度条2
- //C#进度条实现实例
- {
- 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) + "%";
- }
- //C#进度条实现实例
- 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
- { //C#进度条实现实例
- 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();
- //请求暂停后台操作
- }
- } //C#进度条实现实例
- }
C#进度条实现实例的相关内容就向你介绍到这里,希望对你了解和学习C#进度条实现有所帮助。
【编辑推荐】
分享文章:C#进度条实现实例
地址分享:http://www.shufengxianlan.com/qtweb/news42/448192.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联