多线程中如何操作textBox?
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread tdTmp = new Thread(new ThreadStart(new Hebf(ref textBox1).test));
tdTmp.Start();
tdTmp.Join();
}
}
class Hebf
{
TextBox textBox_Main;
public Hebf(ref TextBox textBox_MainTmp)
{
this.textBox_Main = textBox_MainTmp;
}
public void test()
{
textBox_Main.Text += "asad ";//这样出错
}
}
不同的class里怎么操作?
------解决方案--------------------change
Thread tdTmp = new Thread(new ThreadStart(new Hebf(ref textBox1).test));
to
Hebf hebf = new Hebf(ref textBox1);
Thread tdTmp = new Thread(new ThreadStart(hebf.test));
------解决方案--------------------//这样测试看看
private void button1_Click(object sender, EventArgs e)
{
Thread tdTmp = new Thread(new ThreadStart(new Hebf(textBox1).test));
tdTmp.Start();
//tdTmp.Join();
}
class Hebf
{
TextBox textBox_Main;
public Hebf(TextBox textBox_MainTmp)
{
textBox_Main = textBox_MainTmp;
}
public void test()
{
textBox_Main.Invoke(new EventHandler(DoInvoke));
}
private void DoInvoke(object sender, EventArgs e)
{
textBox_Main.Text += "asad ";
}
}