Well, there are two separate issues here:
- Updating the UI from a different class
- Updating the UI from a different thread
The two are almost completely orthogonal. Once you've solved each of them in isolation, you can put the two together.
If there are well-understood ways in which the UI needs to be changed (e.g. updating the text in a status bar or something like that) I would write methods in the UI class itself to do that, and just call those methods from the other class. Alternatively, you can expose the individual control of the UI - preferably through properties - and then use Control.Invoke
in the normal way from the other class, e.g.
MethodInvoker action = () => form.StatusLabel.Text = "Finished";
form.BeginInvoke(action);
So long as you call Invoke
on a control which "lives" on the same thread as all the controls you touch, it doesn't matter which one you use... so an alternative could be:
Label label = form.StatusLabel;
MethodInvoker action = () => label.Text = "Finished";
label.BeginInvoke(action);
One difference here is that in this latter code, the StatusLabel
property is being evaluated in the worker thread - which could lead to race conditions if your UI is changing it to refer to different labels at different times. I would generally prefer the first approach.
(I agree with icktoofay's comment about BeginInvoke
being preferable unless you really need to wait, by the way... although you do then need to be careful about changes to any variables captured by anonymous functions.)