Do you guys see any potential problems with the following InvokeRequired strategy for simple Actions?
public static class ControlInvokeRequiredExtensionMethods
{
public static void ExecuteOnUIThread(this Control myControl, Action action)
{
if (myControl.InvokeRequired)
{
myControl.BeginInvoke(action);
}
else { action(); }
}
public static void ExecuteOnUIThread<T>(this Control myControl, Action<T> action, T parameter)
{
if (myControl.InvokeRequired)
{
myControl.BeginInvoke(action, new object[] { parameter });
}
else { action(parameter); }
}
// etc...
}
Sample usage:
RichTextBox textBox = new RichTextBox();
public void WriteMessage(string message)
{
textBox.ExecuteOnUIThread(textBox.AppendText, message);
}
Found
this alternative, but would prefer to avoid AOP. Seems to me like an Extension Method strikes a nice balance between simplicity and transparency.