Tech Off Post

Single Post Permalink

View Thread: I was told today to not worry cross-thread execution when...
  • User profile image

    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.Text = "Keyboard acquired"
            End If
     End Sub

    Check out the invokerequired property of the control class.

    This is a cross-cutting concern, no? I do this with the following extension methods:

    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?