Tech Off Post

Single Post Permalink

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

    dcuccia said:
    gadget said:
    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?
    In your original post, I found it interesting you called this InvokeOnUIThread, but the implementation did a BeginInvoke.  Very different operational behaviors there.

    This is mostly a matter of performance.  The normal pattern has slightly better performance, since we're only calling through a delegate when we absolutely must.  I'm not 100% sure about how things are implemented under the covers, but I'd suspect you'd be better off just calling Invoke (or BeginInvoke) without checking InvokeRequired, rather than wrapping this in a InvokeOnUIThread method.