3
Answers

Inter-thread operation not valid

Ken H

Ken H

10y
1k
1
hello,
     I try to use the Form Controls multithreaded call, the following error occurred:
      Inter-thread operation not valid: access it from not create controls "textBox2" thread. 
My codes: 
private void btnMT_Click(object sender, EventArgs e) {


MessageBox.Show("Now,You still can do some other things.");

SetEnabledForFalse();

Thread t_one = new Thread(new ThreadStart(FunOne));

Thread t_two = new Thread(new ThreadStart(FunTwo));


t_one.Start();

t_two.Start();

SetEnabledForTrue();



}
private void FunOne() {

for (int i = 0; i < 100; i++)

{

textBox1.AppendText(i.ToString()+" ");

Thread.Sleep(500);

}


}
private void FunTwo() {


for (int i = 0; i < 100; i++)

{

textBox2.AppendText(i.ToString() + " ");

Thread.Sleep(500);

}

}
private void SetEnabledForTrue() {

textBox1.Enabled = true;

textBox2.Enabled = true;

btnMT.Enabled = true;

}
private void SetEnabledForFalse(){

textBox1.Enabled = false;

textBox2.Enabled = false;

btnMT.Enabled = false;

}
private void btnOther_One_Click(object sender, EventArgs e){

MessageBox.Show("This btnOther_One_Click,Working...");

}
private void btnOther_two_Click(object sender, EventArgs e){

MessageBox.Show("This btnOther_two_Click,Working...");

}
Thanks. 
 
Answers (3)