I have the folowing code writing to a textbox:
for (int ii = 0; ii < 5; ii++)
{
textBox1.Text += String.Format("Writing Line {0}{1}", ii + 1, System.Environment.NewLine);
}
I get the expected output off:
Writing Line 1
Writing Line 2
Writing Line 3
Writing Line 4
Writing Line 5
However when I update asyncornusly or from another thread as found here: How to update the GUI from another thread in C#?
for (int ii = 0; ii < 5; ii++)
{
textBox1.BeginInvoke(new Action(() => textBox1.Text += String.Format("Writing Line {0}{1}", ii + 1 , System.Environment.NewLine)));
}
I get
Writing Line 5
Writing Line 5
Writing Line 5
Writing Line 5
Writing Line 5
The last line is printed 5 times where I expect the output to match the single-thread synchronous output?
What do I need to change to match the first output from the second thread?