I have gotten a bit lazy(it’s sometimes good) and started updating WinForms UI by invoking a callback without checking InvokeRequired first.
Are there a performance issues or considerations that I should be aware of?
private delegate void SetStatusEventHandler(string statusMessage); private void SetStatus(string statusMessage) { Invoke((MethodInvoker) (() => { resultLabel.Text = statusMessage; })); // - vs - if (InvokeRequired) { SetStatusEventHandler cb = SetStatus; Invoke(cb, statusMessage); } else { resultLabel.Text = statusMessage; } }
[EDIT]: Most of times that a method that calls ‘invoke’ will be called at most like say 10~20 times a second with a wide interval inbetween.
[UPDATE] Settled with the following extension method
public static class SmartInvoker { public static void InvokeHandler(this Control control, MethodInvoker del) { if (control.InvokeRequired) { control.Invoke(del); return; } del(); } } ... private void SetStatus(string statusMessage) { this.InvokeHandler(() => resultLabel.Text = statusMessage); }
I guess finding out how to manage extension method classes is another topic I need to dig in. Thank you for your help
EDIT: See the comments for debate about the whole posting vs immediately dispatching malarky.
Either way, my answer is actually the same: unless this is happening hugely often unnecessarily (i.e. most of the time you’re on the UI thread to start with, and it’s attached to something like a mouse movement handler) I wouldn’t worry. It certainly makes the code simpler. If this is going to be invoked very often, I’d measure and test more 🙂
Invokeis faster with anEventHandlerorMethodInvokerdelegate than with others. I don’t know if there’s any difference between the two – you may want to check.You can make this even simpler by writing an extension method, e.g.
Then you can make your code:
or
That way you don’t need to specify the delegate type.