代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
winform进度条
作者:
Dezai.CN
/ 发布于
2013/1/4
/
1604
进度条页面: //=============================================================================== // 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(); } //工作中执行进度更新 public void OnProgressChanged(object sender, ProgressChangedEventArgs e) { progressWork.Value = e.ProgressPercentage; } private void btnClose_Click(object sender, EventArgs e) { Close(); } } } 主界面 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<string> leftList = new BindingList<string>(); private IList<string> rightList = new BindingList<string>(); 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) { //显示进度条 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 //线程的主要功能是处理事件 //开启线程执行工作 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<string> list = e.Argument as IList<string>; 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); } //工作完成状态 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(); } } }
试试其它关键字
进度条
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3