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") + " 數據更新成功"; } } }
