Suppose a GUI (C#, WinForms) that performs work and is busy for several seconds. It will still have buttons that need to remain accessible, labels that will change, progress bars, etc.
I’m using this approach currently to change the GUI when busy:
//Generic delegates
private delegate void SetControlValue<T>(T newValue);
//...
public void SetStatusLabelMessage(string message)
{
if (StatusLabel.InvokeRequired)
StatusLabel.BeginInvoke(new SetControlValue<string>(SetStatusLabelMessage, object[] { message });
else
StatusLabel.Text = message;
}
I’ve been using this like it’s going out of style, yet I’m not quite certain this is proper. Creating the delegate (and reusing it) makes the whole thing cleaner (for me, at least), but I must know that I’m not creating a monster…
Another alternative is to use a BackgroundWorker and use ReportProgress when you need to update the GUI. This will handle the Invoke for you so that you don’t have to worry about it.