how the richtextbox is written multithreadingly?

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

Herfried K. Wagner [MVP]

* =?Utf-8?B?emJjb25n?= said:
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.

Have a look at 'Control.InvokeRequired', 'Control.Invoke', and
'Control.BeginInvoke'. Some articles about Windows Forms +
multithreading:

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms01232003.asp>
 
S

Shaival Trivedi

your code is perfect it will run perfectly..

but still try this

delegate void rptdel();

private void rpt()

{

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

{

this.richTextBox1.AppendText("hello world");

}

}

private void button2_Click(object sender, System.EventArgs e)

{rptdel rp = new rptdel(rpt);


rp.BeginInvoke(null,null);


}


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