C# 通过委托控制进度条以及多线程更新控件
- 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;
- using System.Threading;
- namespace Demo0004
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //线程开始的时候调用的委托
- private delegate void maxValueDelegate(int maxValue);
- //线程执行中调用的委托
- private delegate void nowValueDelegate(int nowValue);
- private void button1_Click(object sender, EventArgs e)
- {
- ThreadMethod method = new ThreadMethod();
- //先订阅一下事件
- method.threadStartEvent += new EventHandler(method_threadStartEvent);
- method.threadEvent += new EventHandler(method_threadEvent);
- method.threadEndEvent += new EventHandler(method_threadEndEvent);
- Thread thread = new Thread(new ThreadStart(method.runMethod));
- thread.Start();
- }
- /// <summary>
- /// 线程开始事件,设置进度条最大值
- /// 但是我不能直接操作进度条,需要一个委托来替我完成
- /// </summary>
- /// <param name="sender">ThreadMethod函数中传过来的最大值</param>
- /// <param name="e"></param>
- void method_threadStartEvent(object sender, EventArgs e)
- {
- int maxValue = Convert.ToInt32(sender);
- maxValueDelegate max = new maxValueDelegate(setMax);
- this.Invoke(max, maxValue);
- }
- /// <summary>
- /// 线程执行中的事件,设置进度条当前进度
- /// 但是我不能直接操作进度条,需要一个委托来替我完成
- /// </summary>
- /// <param name="sender">ThreadMethod函数中传过来的当前值</param>
- /// <param name="e"></param>
- void method_threadEvent(object sender, EventArgs e)
- {
- int nowValue = Convert.ToInt32(sender);
- nowValueDelegate now = new nowValueDelegate(setNow);
- this.Invoke(now, nowValue);
- }
- /// <summary>
- /// 线程完成事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void method_threadEndEvent(object sender, EventArgs e)
- {
- MessageBox.Show("执行已经完成!");
- }
- /// <summary>
- /// 我被委托调用,专门设置进度条最大值的
- /// </summary>
- /// <param name="maxValue"></param>
- private void setMax(int maxValue)
- {
- this.progressBar1.Maximum = maxValue;
- }
- /// <summary>
- /// 我被委托调用,专门设置进度条当前值的
- /// </summary>
- /// <param name="nowValue"></param>
- private void setNow(int nowValue)
- {
- this.progressBar1.Value = nowValue;
- }
- }
- public class ThreadMethod
- {
- /// <summary>
- /// 线程开始事件
- /// </summary>
- public event EventHandler threadStartEvent;
- /// <summary>
- /// 线程执行时事件
- /// </summary>
- public event EventHandler threadEvent;
- /// <summary>
- /// 线程结束事件
- /// </summary>
- public event EventHandler threadEndEvent;
- public void runMethod()
- {
- int count = 100; //执行多少次
- threadStartEvent.Invoke(count, new EventArgs());//通知主界面,我开始了,count用来设置进度条的最大值
- for (int i = 0; i < count; i++)
- {
- Thread.Sleep(100);//休息100毫秒,模拟执行大数据量操作
- threadEvent.Invoke(i, new EventArgs());//通知主界面我正在执行,i表示进度条当前进度
- }
- threadEndEvent.Invoke(new object(), new EventArgs());//通知主界面我已经完成了
- }
- }
- }
- 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;
- using System.Threading;
- namespace Demo0004
- {
- public partial class Form2 : Form
- {
- //在下载窗体上面 建一个委托
- public delegate void ChangeProgress(int value); //进度条
- public delegate void ChangeButton(int value); //按钮
- //创建上面的委托的变量
- public ChangeProgress changeProgerss;
- public ChangeButton changebtn;
- public Form2()
- {
- InitializeComponent();
- //为这个委托变量赋值
- changeProgerss = FunChangeProgress;
- changebtn = FunChangebutton;
- }
- //通过创建工作线程消除用户界面线程的阻塞问题
- private void button1_Click(object sender, EventArgs e)
- {
- button1.Enabled = false;
- //新创建一个线程
- System.Threading.Thread thr = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Download));
- thr.Start();
- }
- //线程方法 一定要是object 类型参数 同时返回值是void
- private void Download(object obj)
- {
- for (int i = 0; i <= 100; i++)
- {
- //执行委托 更新按钮 -重点
- this.button1.Invoke(changebtn, i);
- //执行委托 更新进度条 -重点
- this.progressBar1.Invoke(changeProgerss, i);
- System.Threading.Thread.Sleep(100);
- }
- }
- //更新进度条
- public void FunChangeProgress(int value)
- {
- progressBar1.Value = value;
- }
- //更新按钮
- public void FunChangebutton(int value)
- {
- if (value == 100)
- {
- button1.Text = "开始新进程";
- button1.Enabled = true;
- }
- else
- {
- //相除保留两位小数 且四舍五入 Math.Round(1.00 * value / 100, 2,MidpointRounding.AwayFromZero)
- button1.Text = Math.Round(1.00 * value / 100, 2,MidpointRounding.AwayFromZero) * 100 + "%";
- }
- }
- //窗体关闭 强制退出 销毁所有相关进程
- private void Form2_FormClosing(object sender, FormClosingEventArgs e)
- {
- //强制退出 销毁进程
- System.Environment.Exit(System.Environment.ExitCode);
- this.Dispose();
- this.Close();
- }
- }
- }
- 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 Demo0004
- {
- public partial class Form3 : Form
- {
- public delegate void ChangeStatus();
- //创建上面的委托的变量
- public ChangeStatus changestatus;
- public Form3()
- {
- InitializeComponent();
- }
- private void Form3_Load(object sender, EventArgs e)
- {
- //使用Timer组件实现多线程定时同步
- System.Timers.Timer t = new System.Timers.Timer(3000); //实例化Timer类,设置间隔时间单位毫秒;
- t.Elapsed += new System.Timers.ElapsedEventHandler(UpdateWork); //到达时间的时候执行事件;
- t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
- t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
- changestatus = FunChangeStatus;
- }
- private void UpdateWork(object source, System.Timers.ElapsedEventArgs e)
- {
- this.Invoke(changestatus);
- }
- //更新
- public void FunChangeStatus()
- {
- #region 更新开始
- //更新方法
- #endregion
- lbtimer.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 数据更新成功";
- }
- }
- }

C# 通过委托控制进度条以及多线程更新控件的更多相关文章
- 进度条的使用 Progress控件
MFC编程实例二:进度条的使用 2011-03-22 09:09:09| 分类: C++(C语言) | 标签:进度 nlower nupper 添加 mfc |字号 订阅 本人用的 ...
- 安卓MP3播放器开发实例(3)之进度条和歌词更新的实现
上一次谈了音乐播放的实现,这次说下最复杂的进度条和歌词更新.因为须要在播放的Activity和播放的Service间进行交互,所以就涉及了Activity对Service的绑定以及绑定后数据的传输,这 ...
- C# winform编程中多线程操作控件方法
private void Form1_Load(object sender, EventArgs e) { Thread newthread = new Thread(new ThreadStart( ...
- winform多线程调用控件
对多线程操作控件的理解: 控件不能被非创造他的线程修改.需调用控件.beginvoke,注入UI线程.控件.beginvoke会把操作加入UI线程,阻塞画面响应.不要把耗时的计算放在控件.beginv ...
- COM组件开发实践(八)---多线程ActiveX控件和自动调整ActiveX控件大小(下)
源代码下载:MyActiveX20081229.rar 声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改 ...
- COM组件开发实践(七)---多线程ActiveX控件和自动调整ActiveX控件大小(上)
声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改而来,因此同样遵循Code Project Open L ...
- [寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件
RichTextEx一款通过HTML标签控制文字样式的富文本控件 原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net ...
- C#创建子线程,子线程使用委托更新控件
一.背景 由于在窗体程序中通过点击一个button按键后需要更新TreeView控件的内容,由于等待时间比较长,主程序无法一起在那边等待,需要去处理其它的事情,所以就需要创建新的子线程来处理.因为主线 ...
- C# 通过线程来控制进度条(转)--讲解多线程对界面的操作
// 通过创建委托解决传递参数问题 private void _btnRun_Click( object sender, System.EventArgs e ) { RunTaskDelegate ...
随机推荐
- resin的基本操作
1.什么是resin? resin是CAUCHO公司的产品,是一个非常流行的支持servlets和jsp的引擎,速度非常快.Resin本身包含了一个支持HTTP/1.1的WEB服务器.虽然它可 ...
- JS的prototype的共享机制分析
function Super(){ } Super.prototype.aaa=[1,2,3]; Super.prototype.bbb=1; function Sub(){ Super.call(t ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- IntelliJ IDEA currently
https://www.jetbrains.com/help/idea/2016.2/creating-a-project-from-scratch.html https://www.jetbrain ...
- 轮播图切换 纯html+js+css
如图所示. 该图片切换特效实现很简单,而且兼容性很好. html页面如下 复制代码代码如下: <div class="wrapper"> <div id=&quo ...
- 基于window.onerror事件 建立前端错误日志
QA不是万能的,用户的浏览环境非常复杂,很多情况无法靠测试用例去覆盖,所以最好建立一个前端错误日志,在真实用户端收集bug. try&catch是一个捕获前端错误的常见方法,比如: { //给 ...
- linux服务之varnish
https://www.varnish-cache.org/installation/redhatvarnish是现在很流行的一个HTTP(80)缓存加速解决方案,varnish是基于内存的缓存加速. ...
- sublime个人快捷键
ctrl+alt+f = 代码格式化(html,js) ctrl+d = 选中相同内容 alt+shift+w = 为内容添加新标签 ctrl+shift+a = 选择标签内的内容(再按一 ...
- javascript中href和replace比较
在使用javascript的时候,有时候对于经常使用的方法太熟悉而忽略了他们之间原理的细微差别.举例如下:window.location.href,window.location.replace. ...
- JavaScript兼容性问题
Firefox浏览器不支持js的innerText属性: