C# 通过委托控制进度条以及多线程更新控件

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace Demo0004  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         //线程开始的时候调用的委托  
  21.         private delegate void maxValueDelegate(int maxValue);  
  22.         //线程执行中调用的委托  
  23.         private delegate void nowValueDelegate(int nowValue);  
  24.   
  25.         private void button1_Click(object sender, EventArgs e)  
  26.         {  
  27.             ThreadMethod method = new ThreadMethod();  
  28.             //先订阅一下事件  
  29.             method.threadStartEvent += new EventHandler(method_threadStartEvent);  
  30.             method.threadEvent += new EventHandler(method_threadEvent);  
  31.             method.threadEndEvent += new EventHandler(method_threadEndEvent);  
  32.   
  33.             Thread thread = new Thread(new ThreadStart(method.runMethod));  
  34.             thread.Start();  
  35.         }  
  36.   
  37.         /// <summary>  
  38.         /// 线程开始事件,设置进度条最大值  
  39.         /// 但是我不能直接操作进度条,需要一个委托来替我完成  
  40.         /// </summary>  
  41.         /// <param name="sender">ThreadMethod函数中传过来的最大值</param>  
  42.         /// <param name="e"></param>  
  43.         void method_threadStartEvent(object sender, EventArgs e)  
  44.         {  
  45.             int maxValue = Convert.ToInt32(sender);  
  46.             maxValueDelegate max = new maxValueDelegate(setMax);  
  47.             this.Invoke(max, maxValue);  
  48.         }  
  49.   
  50.         /// <summary>  
  51.         /// 线程执行中的事件,设置进度条当前进度  
  52.         /// 但是我不能直接操作进度条,需要一个委托来替我完成  
  53.         /// </summary>  
  54.         /// <param name="sender">ThreadMethod函数中传过来的当前值</param>  
  55.         /// <param name="e"></param>  
  56.         void method_threadEvent(object sender, EventArgs e)  
  57.         {  
  58.             int nowValue = Convert.ToInt32(sender);  
  59.             nowValueDelegate now = new nowValueDelegate(setNow);  
  60.             this.Invoke(now, nowValue);  
  61.         }  
  62.   
  63.         /// <summary>  
  64.         /// 线程完成事件  
  65.         /// </summary>  
  66.         /// <param name="sender"></param>  
  67.         /// <param name="e"></param>  
  68.         void method_threadEndEvent(object sender, EventArgs e)  
  69.         {  
  70.             MessageBox.Show("执行已经完成!");  
  71.         }  
  72.   
  73.         /// <summary>  
  74.         /// 我被委托调用,专门设置进度条最大值的  
  75.         /// </summary>  
  76.         /// <param name="maxValue"></param>  
  77.         private void setMax(int maxValue)  
  78.         {  
  79.             this.progressBar1.Maximum = maxValue;  
  80.         }  
  81.   
  82.         /// <summary>  
  83.         /// 我被委托调用,专门设置进度条当前值的  
  84.         /// </summary>  
  85.         /// <param name="nowValue"></param>  
  86.         private void setNow(int nowValue)  
  87.         {  
  88.             this.progressBar1.Value = nowValue;  
  89.         }  
  90.     }  
  91.   
  92.   
  93.     public class ThreadMethod  
  94.     {  
  95.         /// <summary>  
  96.         /// 线程开始事件  
  97.         /// </summary>  
  98.         public event EventHandler threadStartEvent;  
  99.         /// <summary>  
  100.         /// 线程执行时事件  
  101.         /// </summary>  
  102.         public event EventHandler threadEvent;  
  103.         /// <summary>  
  104.         /// 线程结束事件  
  105.         /// </summary>  
  106.         public event EventHandler threadEndEvent;  
  107.   
  108.         public void runMethod()  
  109.         {  
  110.             int count = 100;      //执行多少次  
  111.             threadStartEvent.Invoke(count, new EventArgs());//通知主界面,我开始了,count用来设置进度条的最大值  
  112.             for (int i = 0; i < count; i++)  
  113.             {  
  114.                 Thread.Sleep(100);//休息100毫秒,模拟执行大数据量操作  
  115.                 threadEvent.Invoke(i, new EventArgs());//通知主界面我正在执行,i表示进度条当前进度  
  116.             }  
  117.             threadEndEvent.Invoke(new object(), new EventArgs());//通知主界面我已经完成了  
  118.         }  
  119.     }  
  120. }  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace Demo0004  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         //在下载窗体上面 建一个委托  
  16.         public delegate void ChangeProgress(int value); //进度条  
  17.         public delegate void ChangeButton(int value); //按钮  
  18.         //创建上面的委托的变量  
  19.         public ChangeProgress changeProgerss;  
  20.         public ChangeButton changebtn;  
  21.   
  22.         public Form2()  
  23.         {  
  24.             InitializeComponent();  
  25.             //为这个委托变量赋值  
  26.             changeProgerss = FunChangeProgress;  
  27.             changebtn = FunChangebutton;  
  28.         }  
  29.   
  30.         //通过创建工作线程消除用户界面线程的阻塞问题   
  31.         private void button1_Click(object sender, EventArgs e)  
  32.         {  
  33.             button1.Enabled = false;  
  34.             //新创建一个线程  
  35.             System.Threading.Thread thr = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Download));  
  36.             thr.Start();              
  37.         }  
  38.   
  39.         //线程方法 一定要是object 类型参数 同时返回值是void  
  40.         private void Download(object obj)  
  41.         {  
  42.             for (int i = 0; i <= 100; i++)  
  43.             {  
  44.                 //执行委托 更新按钮  -重点  
  45.                 this.button1.Invoke(changebtn, i);  
  46.                 //执行委托 更新进度条  -重点  
  47.                 this.progressBar1.Invoke(changeProgerss, i);  
  48.                 System.Threading.Thread.Sleep(100);  
  49.             }  
  50.         }  
  51.   
  52.         //更新进度条  
  53.         public void FunChangeProgress(int value)  
  54.         {  
  55.             progressBar1.Value = value;  
  56.         }  
  57.   
  58.         //更新按钮  
  59.         public void FunChangebutton(int value)  
  60.         {  
  61.             if (value == 100)  
  62.             {  
  63.                 button1.Text = "开始新进程";  
  64.                 button1.Enabled = true;  
  65.             }  
  66.             else  
  67.             {  
  68.                 //相除保留两位小数 且四舍五入 Math.Round(1.00 * value / 100, 2,MidpointRounding.AwayFromZero)  
  69.                 button1.Text = Math.Round(1.00 * value / 100, 2,MidpointRounding.AwayFromZero) * 100 + "%";  
  70.             }  
  71.         }  
  72.   
  73.         //窗体关闭 强制退出 销毁所有相关进程  
  74.         private void Form2_FormClosing(object sender, FormClosingEventArgs e)  
  75.         {  
  76.             //强制退出 销毁进程  
  77.             System.Environment.Exit(System.Environment.ExitCode);  
  78.             this.Dispose();  
  79.             this.Close();  
  80.         }  
  81.     }  
  82. }  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace Demo0004  
  11. {  
  12.     public partial class Form3 : Form  
  13.     {  
  14.         public delegate void ChangeStatus();  
  15.         //创建上面的委托的变量    
  16.         public ChangeStatus changestatus;  
  17.         public Form3()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void Form3_Load(object sender, EventArgs e)  
  23.         {  
  24.             //使用Timer组件实现多线程定时同步  
  25.             System.Timers.Timer t = new System.Timers.Timer(3000);   //实例化Timer类,设置间隔时间单位毫秒;   
  26.             t.Elapsed += new System.Timers.ElapsedEventHandler(UpdateWork); //到达时间的时候执行事件;     
  27.             t.AutoReset = true;   //设置是执行一次(false)还是一直执行(true);     
  28.             t.Enabled = true;     //是否执行System.Timers.Timer.Elapsed事件;     
  29.             changestatus = FunChangeStatus;    
  30.         }  
  31.   
  32.         private void UpdateWork(object source, System.Timers.ElapsedEventArgs e)  
  33.         {  
  34.             this.Invoke(changestatus);  
  35.         }  
  36.   
  37.         //更新  
  38.         public void FunChangeStatus()  
  39.         {  
  40.             #region 更新开始  
  41.             //更新方法  
  42.             #endregion  
  43.             lbtimer.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 数据更新成功";  
  44.         }  
  45.     }  
  46. }  
posted on 2016-09-06 15:37  莫水千流  阅读(6493)  评论(0编辑  收藏  举报