日期:2014-05-18 浏览次数:20836 次
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 MutiThread { public partial class Form1 : Form { public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } protected delegate void dShowPic(bool s); protected void ShowPic(bool s) { this.pictureBox1.Visible = s; } protected void RunShowPic() { bool s = this.pictureBox1.Visible; if (InvokeRequired) { this.Invoke(new dShowPic(ShowPic), !s); } } protected delegate void dSetVal(int val); protected void SetVal(int val) { progressBarX1.Value = val; } protected void RunSetVal() { Thread thp = new Thread(new ThreadStart(RunShowPic)); thp.Start(); int v = progressBarX1.Value; while (progressBarX1.Value < 100) { Thread.Sleep(100); if (InvokeRequired) { lock (progressBarX1) { this.Invoke(new dSetVal(SetVal), v++); } } } thp = new Thread(new ThreadStart(RunShowPic)); thp.Start(); } private void buttonX1_Click(object sender, EventArgs e) { Thread th = new Thread(new ThreadStart(RunSetVal)); th.Start(); this.labelX1.Text = "正在执行中..."; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); } } }