views:

116

answers:

2

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
}
+7  A: 

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.)

Reed Copsey
+1  A: 

It is fine. Everything in one place. Easy to understand.

AMissico