Recursive Invocation: Poor Style?
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 myMe开发者_高级运维thod(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.
精彩评论