I have an application wherein two threads write asynchronously to a single textbox. It works, except that the second thread to write to the textbox overwrites the line that the first thread just wrote. Any thoughts or insight into the problem would be greatly appreciated. I am using Microsoft Visual C# 2008 Express Edition. Thanks.
delegate void SetTextCallback(string text);
private void SetText(string text)
{
this.textBox1.Text += text;
this.textBox1.Select(textBox1.Text.Length, 0);
this.textBox1.ScrollToCaret();
}
private void backgroundWorkerRx_DoWork(object sender, DoWorkEventArgs e)
{
string sText = "";
// Does some receive work and builds sText
if (textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { sText });
}
else
{
SetText(sText);
}
}
EDIT: This might not solve the problem, but you might want to handle the
ProgressChangedevent of the BackgroundWorkers and set the text there.For example:
ProgressChanged is fired on the UI thread, so you don’t need to call
Invoke.By the way, you should probably rename
SetTexttoAppendTextto make the code clearer.Also, you can use the built-in delegate
Action<String>instead of making your ownSetTextCallbackdelegate type.EDIT: Also, you should probably move the
InvokeRequiredcheck toSetText.For example: