You have to be careful with the lambda and BeginInvoke functions. I had code that led to all kinds of weird behavior.
MyThing thing; while( GetThing(ref thing)) { control.BeginInvoke((Action)(() => control.Text = thing.ToString())); }
The problem is that thing not evaluated when creating a lambda function. It is evaluated when performing the lamdba function. But it is tied to a variable that changes at the same time in the producer thread.
You can fix this problem by declaring a copy of the thing local variable
MyThing thing; while( GetThing(ref thing)) { MyThing thing_x = thing; control.BeginInvoke((Action)(() => control.Text = thing_x.ToString())); }
Or you can put a BeginInvoke lesson in a wrapper
MyThing thing; while( GetThing(ref thing)) { SetText(thing); } void SetText(MyThing thing) control.BeginInvoke((Action)(() => control.Text = thing.ToString())); }
source share