multithread to richtextbox

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!
 
H

Henrik Dahl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top