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
ProgressChanged
event 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
SetText
toAppendText
to make the code clearer.Also, you can use the built-in delegate
Action<String>
instead of making your ownSetTextCallback
delegate type.EDIT: Also, you should probably move the
InvokeRequired
check toSetText
.For example:
I agree with SLaks that you should be using the BackgroundWorker more properly. But to "fix" the code supplied, one issue is with the Invoke call... invocation needs to call upon the same method checking for requirement as to align the thread to the form's creator. I typically do something similar to the following (the argument usage may not be compilable but the rest is fine). Honestly, there should most likely only need to be one handler since only one thread can write at a time.
Try putting a lock over the section of the code that you believe you are having concurrency issues with.
Also, try using AppendText instead of manually concatenating the strings.