This is a cross-cutting concern, no? I do this with the following extension methods:gadget said:I check inside the method to see if an invoke is required. If so have the delegate make a recursive call to the method, otherwise perform the action.
This is in vb but should be clear.Public Sub ReadKeyboard()
If Me.InvokeRequired Then
Dim x As Action = AddressOf ReadKeyboard
Me.Invoke(x)
Else
me.Text = "Keyboard acquired"
End If
End Sub
Check out the invokerequired property of the control class.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx
public static void InvokeOnUIThread( this Control myControl, Action action );
public static void InvokeOnUIThread<T>( this Control myControl, Action<T> action, T parameter );
which would be used as follows:
this.InvokeOnUIThread( () => myControl.Text = "Keyboard acquired" );
Asked for feeback on this earlier...what do you guys think?