Hi all,
I sometimes get this error when I forget that controls cannot be accessed across threads:
What many people do is this:
- private void UpdateLabel(string text)
- {
- this.Invoke(new Action(() => this.someLabel.Text = text));
- }
This is not the right pattern. You don’t always HAVE to invoke. You are better off by checking if you have to invoke it on the underlying thread, and if not, just execute it directly. It’s all about correctness
The right pattern would be:
- private void UpdateLabel(string text)
- {
- if(this.InvokeRequired)
- {
- this.Invoke(new Action(() => this.UpdateLabel(text)));
- return;
- }
- this.someLabel.Text = text;
- }
Good luck
-Kristof