典型的多线程操作界面的例子
2008-08-23 14:27 业余爱好者 阅读(283) 评论(0) 编辑 收藏 举报
1public partial class Form1 : Form
2{
3 public Form1()
4 {
5 InitializeComponent();
6 }
7 private void button1_Click(object sender, EventArgs e)
8 {
9 this.progressBar1.Maximum = 9000;
10 progressBar1.Minimum = 0;
11 progressBar1.MarqueeAnimationSpeed = 1000;
12 Thread tt = new Thread(new ThreadStart(LongProcess));
13 tt.IsBackground = true;
14 tt.Start();
15 }
16 delegate void UpProgressDelegate(int value);
17 void ProgressIncrease(int value)
18 {
19 this.progressBar1.Value = value;
20 }
21 void LongProcess()
22 {
23 for (int i = 0; i < 9000; i++)
24 {
25 //Do something
26 Thread.Sleep(1);
27 //Porogress Increase
28 if (progressBar1.InvokeRequired)
29 {
30 progressBar1.Invoke(new UpProgressDelegate(ProgressIncrease),new object[] { i });
31 }
32 else
33 {
34 ProgressIncrease(i);
35 }
36 }
37 }
38}
2{
3 public Form1()
4 {
5 InitializeComponent();
6 }
7 private void button1_Click(object sender, EventArgs e)
8 {
9 this.progressBar1.Maximum = 9000;
10 progressBar1.Minimum = 0;
11 progressBar1.MarqueeAnimationSpeed = 1000;
12 Thread tt = new Thread(new ThreadStart(LongProcess));
13 tt.IsBackground = true;
14 tt.Start();
15 }
16 delegate void UpProgressDelegate(int value);
17 void ProgressIncrease(int value)
18 {
19 this.progressBar1.Value = value;
20 }
21 void LongProcess()
22 {
23 for (int i = 0; i < 9000; i++)
24 {
25 //Do something
26 Thread.Sleep(1);
27 //Porogress Increase
28 if (progressBar1.InvokeRequired)
29 {
30 progressBar1.Invoke(new UpProgressDelegate(ProgressIncrease),new object[] { i });
31 }
32 else
33 {
34 ProgressIncrease(i);
35 }
36 }
37 }
38}