Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method executing twice after being invoked

I'm having a "invoke" issue that I can't resolve. I'll try to be as thorough in my description as possible, but I'm new to this so bear with me and let me know if you need more information.

I've got a background thread running that when prompted will disable a bunch of check boxes on a form created on the main thread. In order to do this I need to safely cross-thread using an invoke and a delegate but I must be doing it incorrectly. Bottom line, when I check this in the debugger i see that it runs through the ACTION portion of the code twice if InvokeRequired. I can get around this by bracketing ACTION with an else, and although it won't run through the else twice it does still try to go through the method again.

delegate void ManualCurtainShuttoffHandler();
public void ManualCurtainShutoff()
{
    if (InvokeRequired)                                    
    {
        Invoke(new ManualCurtainShuttoffHandler(ManualCurtainShutoff));           
    }
    // ACTION: Disable check boxes
}

I'd just like to know why it runs through the method two times. Let me know if you need any more information and I'd be happy to share it with you.

like image 409
Display Name Avatar asked Oct 25 '25 10:10

Display Name


1 Answers

Just because you call Invoke, it doesn't stop execution of the current method. A quick and simple solution is to simply return after calling Invoke:

delegate void ManualCurtainShuttoffHandler();
public void ManualCurtainShutoff()
{
    if (InvokeRequired)                                    
    {
        Invoke(new ManualCurtainShuttoffHandler(ManualCurtainShutoff));           
        return;
    }
    // ACTION: Disable check boxes
}

This will skip the rest of the execution of ManualCurtainShutoff that's running on the background thread while still promoting a new execution of the method on the main thread.

like image 178
Chris Sinclair Avatar answered Oct 27 '25 01:10

Chris Sinclair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!