1、 通过跨线程给控件赋值
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(()=> {
for(int i = 0; i < 10; i++)
{
//this.label1.Text = i.ToString();
if (this.label1.InvokeRequired)
{
this.label1.Invoke(new Action<string>(data => this.label1.Text = data), i.ToString());
}
Thread.Sleep(300);
}
});
thread.IsBackground = true;
thread.Start();
}
2、 通过跨线程读取控件的值
private void button2_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() =>
{
this.textBox1.Invoke(new Action<string>(data => this.label1.Text = data), this.textBox1.Text);
});
thread.IsBackground = true;
thread.Start();
}
3、补充
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() =>
{
for (int i = 0; i < 10; i++)
{
//this.BeginInvoke(new Action<string>(data => this.label1.Text = data), i.ToString());
this.BeginInvoke(new Action(() => this.label1.Text = i.ToString() )) ;
Thread.Sleep(300);
}
});
thread.IsBackground = true;
thread.Start();
}