c# this.Invoke的定义及用法(个人理解的用法)

 C# this.invoke()作用

Invoke()的作用是:在应用程序的主线程上执行指定的委托。一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke();

 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 namespace Invoke_WindowsForm
11 {
12     public partial class Form1 : Form
13     {
14         public Form1()
15         {
16             InitializeComponent();
17         }
18 
19         private void Form1_Load(object sender, EventArgs e)
20         {
21             Thread d = new Thread(new ThreadStart(this.DoSomething));
22             d.Start();
23         }
24         private void DoSomething()
25         {
26             for (int i = 0; i < 100; i++)
27             {
28                 this.Invoke(new MethodInvoker(() => {
29                     Thread.Sleep(100);
30                     this.label1.Text = i.ToString();
31                 }));
32             }
33         }
34     }
35 }

以上代码就是动态改变UI上空间值得方法 主线程会在UI线程和辅助线程之间相互转换 

posted @ 2012-11-04 20:00  Ganler1988  阅读(3859)  评论(0编辑  收藏  举报