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. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM