> Suppose I have a windows form (.exe) that has a load of labels and
> text boxes on it.
[quoted text clipped - 13 lines]
>
> for each control that I want to set the text for?
If BackgroundWorker is sufficient for your needs, I highly recommend it.
It provides a very nice, clean, simple interface for dealing with the
cross-thread issues (assuming you only ever update your UI in the
ProgressChanged or RunWorkerCompleted events).
I will note that the pattern used in the method you posted is, IMHO,
unnecessarily verbose and complicated. It's not harmful to use
Control.Invoke() even if you're already executing on the correct thread.
And more than likely, you already know you're on the wrong thread anyway.
Also, you don't need a separate delegate variable for the call to
Invoke(), even when using a named method. And if you use an anonymous
method, even better.
So, for example, instead of all the rigamarole you posted (no offense
intended...I realize that what you posted is basically the boilerplate
pattern that MSDN recommends), you could do this:
myControl.Invoke((MethodInvoker) delegate { myControl.Text = text; });
You can, of course, wrap that up in a method to make it even simpler:
private void SetText(string text, Control controlName)
{
controlName.Invoke((MethodInvoker) delegate { controlName.Text =
text; });
}
In neither case do you really need all that others stuff.
But, if you can use BackgroundWorker instead, even better. :)
Note: by this I do not mean that you'd use BackgroundWorker _just_ to
update the GUI. It would only make sense to use BackgroundWorker if the
task that you're already putting in another thread can suitable be put as
the DoWork event handler for a BackgroundWorker instead.
Pete