multithread to richtextbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello
i put a richtextbox and a button on the winForm.when the button is clicked,it generate several threads to write to the richtextbox,the code snippet as following:


private void btnSend_Click(object sender, System.EventArgs e)
{
for(int i=0;i<3;i++)
{
Thread thread =new Thread(new ThreadStart(rpt));
thread.Start();
}
}

private void rpt()
{
this.richTextBox1.AppendText("hello world");
}


it throw null reference exception,i wonder how can i write to the richtextbox MULTITHREADINGLY,any code snippet is appreciated.

thank you!
 
By using Form.BeginInvoke.

Also in this case it could be an idea to read and understand the conceptual
foundation. The 3 series article on MSDN on multi threaded WindowsForms
programming could make a good beginning. When you've understood the concepts
you'll know that the richtextbox may only be used by the STA thread which
created it.


Best regards,

Henrik Dahl


zbcong said:
hello
i put a richtextbox and a button on the winForm.when the button is
clicked,it generate several threads to write to the richtextbox,the code
snippet as following:
private void btnSend_Click(object sender, System.EventArgs e)
{
for(int i=0;i<3;i++)
{
Thread thread =new Thread(new ThreadStart(rpt));
thread.Start();
}
}

private void rpt()
{
this.richTextBox1.AppendText("hello world");
}


it throw null reference exception,i wonder how can i write to the
richtextbox MULTITHREADINGLY,any code snippet is appreciated.
 
Back
Top