Below is a rather frightening pattern I sometimes use as a lazy way to do simple invocation. This code makes me feel slightly guilty, even though I'm not sure why. Is this horrifying? Reasonable? Going to blow up in my face later?
public void myMethod(object args)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate
{
myMethod(args);
}));
return;
}
//Do Stuff
}
This is a very common means of making sure that a method is run using the UI thread's Synchronization Context. There is nothing wrong with this.
(On a side note, it's the convention in .NET to use pascal casing for methods, so I would change this to MyMethod
. Given that this question is about style, I feel this is worth mentioning.)
It is fine. Everything in one place. Easy to understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With