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();

}