您的位置:首页 > 编程语言 > C#

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

2012-11-04 20:00 459 查看

C# this.invoke()作用

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

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 Invoke_WindowsForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Thread d = new Thread(new ThreadStart(this.DoSomething));
d.Start();
}
private void DoSomething()
{
for (int i = 0; i < 100; i++)
{
this.Invoke(new MethodInvoker(() => {
Thread.Sleep(100);
this.label1.Text = i.ToString();
}));
}
}
}
}


以上代码就是动态改变UI上空间值得方法 主线程会在UI线程和辅助线程之间相互转换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: