C#进度条实现之异步实例是如何展示C#进度条实现的呢?让我们来看看:
10余年的醴陵网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整醴陵建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“醴陵网站设计”,“醴陵网站推广”以来,每个客户项目都认真落实执行。
C#进度条实现之异步实例进度条页面:
- //====================================
- // Microsoft patterns & practices
- // CompositeUI Application Block
- //====================================
- // Copyright ?Microsoft Corporation.
- //All rights reserved.
- // THIS CODE AND INFORMATION IS
- //PROVIDED "AS IS" WITHOUT WARRANTY
- // OF ANY KIND, EITHER EXPRESSED OR
- //IMPLIED, INCLUDING BUT NOT
- // LIMITED TO THE IMPLIED WARRANTIES
- // OF MERCHANTABILITY AND
- // FITNESS FOR A PARTICULAR PURPOSE.
- //=====================================
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace BackgroudWokerUI
- {
- public partial class ProgressForm : Form
- {
- public ProgressForm()
- {
- InitializeComponent();
- }
- //工作完成后执行的事件
- public void OnProcessCompleted(object sender, EventArgs e)
- {
- this.Close();
- }
- //工作中执行进度更新 ,C#进度条实现之异步实例
- public void OnProgressChanged(
- object sender, ProgressChangedEventArgs e)
- {
- progressWork.Value = e.ProgressPercentage;
- }
- private void btnClose_Click(object sender, EventArgs e)
- {
- Close();
- }
- }
- }
C#进度条实现之异步实例主页面:
- 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;
- //Note You must be careful not to manipulate any user-interface objects
- //in your System.ComponentModel.BackgroundWorker.DoWork event handler.
- //Instead, communicate to the user interface through the
- //System.ComponentModel.BackgroundWorker.ProgressChanged and
- //System.ComponentModel.BackgroundWorker.RunWorkerCompleted events.
- namespace BackgroudWokerUI
- {
- public partial class MainForm : Form
- {
- //BindingList is useful list for UI
- private IList
leftList = new BindingList (); - private IList
rightList = new BindingList (); - private BackgroundWorker worker = null;
- public MainForm()
- {
- InitializeComponent();
- //Databinding here
- listBox1.DataSource = leftList;
- listBox2.DataSource = rightList;
- }
- private void addButton_Click(object sender, EventArgs e)
- {
- if (textBox.Text.Length != 0)
- {
- leftList.Add(textBox.Text);
- textBox.Text = "";
- textBox.Focus();
- }
- }
- private void moveButton_Click(object sender, EventArgs e)
- {
- //显示进度条 ,C#进度条实现之异步实例
- ProgressForm progressForm = new ProgressForm();
- progressForm.Show();
- // Prepare the background worker
- //for asynchronous prime number calculation
- //准备进度条的记数
- worker= new BackgroundWorker();
- // Specify that the background
- //worker provides progress notifications
- //指定提供进度通知
- worker.WorkerReportsProgress = true;
- // Specify that the background worker supports cancellation
- //提供中断功能
- worker.WorkerSupportsCancellation = true;
- // The DoWork event handler is the main
- //work function of the background thread
- //线程的主要功能是处理事件
- //开启线程执行工作 ,C#进度条实现之异步实例
- worker.DoWork += new DoWorkEventHandler(worker_DoWork);
- // Specify the function to use to handle progress
- //指定使用的功能来处理进度
- worker.ProgressChanged +=
- new ProgressChangedEventHandler(worker_ProgressChanged);
- worker.ProgressChanged +=
- new ProgressChangedEventHandler(progressForm.OnProgressChanged);
- // Specify the function to run when the
- //background worker finishes
- // There are three conditions possible
- //that should be handled in this function:
- // 1. The work completed successfully
- // 2. The work aborted with errors
- // 3. The user cancelled the process
- //进度条结束完成工作
- //1.工作完成
- //2.工作错误异常
- //3.取消工作
- worker.RunWorkerCompleted +=
- new RunWorkerCompletedEventHandler(
- worker_RunWorkerCompleted);
- worker.RunWorkerCompleted+=
- new RunWorkerCompletedEventHandler(
- progressForm.OnProcessCompleted);
- //If your background operation requires a parameter,
- //call System.ComponentModel.BackgroundWorker.RunWorkerAsync
- //with your parameter. Inside
- //the System.ComponentModel.BackgroundWorker.DoWork
- //event handler, you can extract the parameter from the
- //System.ComponentModel.DoWorkEventArgs.Argument property.
- //如果进度条需要参数
- //调用System.ComponentModel.BackgroundWorker.RunWorkerAsync
- //传入你的参数至System.ComponentModel.BackgroundWorker.DoWork
- //提取参数
- //System.ComponentModel.DoWorkEventArgs.Argument
- worker.RunWorkerAsync(leftList);
- }
- //单线程执行工作
- private void worker_DoWork(
- object sender, DoWorkEventArgs e)
- {
- MoveList((BackgroundWorker)sender,e);
- }
- //进行转移工作
- private void MoveList(
- BackgroundWorker worker,DoWorkEventArgs e)
- {
- IList
list = e.Argument as IList ; - for (int i = 0; i < list.Count; i++)
- {
- // Check for cancellation
- //检查取消
- if (worker.CancellationPending)
- {
- e.Cancel = true;
- break;
- }
- else
- {
- // This will be handled in the correct thread thanks to the
- // internals of BackgroundWroker and AsyncOperation
- worker.ReportProgress((i + 1) * (100 / list.Count), list[i]);
- // Simulate some time consuming proccess.
- //线程休眠
- Thread.Sleep(500);
- }
- }
- }
- //添加数据至右边listBox
- private void worker_ProgressChanged(
- object sender, ProgressChangedEventArgs e)
- {
- //Add string to the right listBox
- rightList.Add(e.UserState as string);
- }
- //C#进度条实现之异步实例
- //工作完成状态
- private void worker_RunWorkerCompleted(
- object sender, RunWorkerCompletedEventArgs e)
- {
- if (e.Cancelled)
- {
- label.Text = "Cancelled!取消";
- }
- else if (e.Error != null)
- {
- label.Text = "Error!异常";
- }
- else
- {
- label.Text = "Success!完成";
- leftList.Clear();
- }
- }
- //取消中
- private void cancelButton_Click(
- object sender, EventArgs e)
- {
- if (worker.IsBusy)
- {
- label.Text = "Cancelling...";
- //挂起进程
- worker.CancelAsync();
- }
- }
- //返回操作
- private void moveBackButton_Click(
- object sender, EventArgs e)
- {
- foreach (string str in rightList)
- {
- leftList.Add(str);
- }
- rightList.Clear();
- }
- }
- }
C#进度条实现之异步实例的相关内容就向你介绍到这里,希望对你了解和学习C#进度条实现有所帮助。
分享文章:C#进度条实现之异步实例浅析
文章位置:http://www.mswzjz.cn/qtweb/news8/65408.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能