Invoke实现跨线程访问控件

不带参数的跨线程

private
void button1_Click(object sender, EventArgs e)

{

Thread t = new
Thread(() =>

{

Random R = new
Random();

this.Invoke(new
Action(()=> { this.Text = R.NextDouble().ToString(); }));

});

t.Start();

}

注意:Invoke应用在其他线程内部;

带参数的跨线程

private
void button1_Click(object sender, EventArgs e)

{

Action<object> a = new
Action<object>(printf);

this.Invoke(a,“Caption”);

}

public
void printf(object s)

{

this.Text = s.ToString();

}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注