Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 myMethod(object args)
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(delegate
        {
            myMethod(args);
        }));
        return;
    }
    //Do Stuff
}
like image 792
Brian Avatar asked Jan 22 '23 06:01

Brian


2 Answers

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

like image 77
Reed Copsey Avatar answered Feb 05 '23 05:02

Reed Copsey


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

like image 33
AMissico Avatar answered Feb 05 '23 04:02

AMissico