Manually cancelling a Command

Hello,

I am trying to create a Command subclass which listens for an interrupt to cancel the command. When cancelling the command, I have tried using the Cancel() method, but it fails when the command is part of a CommandGroup. Is there a universal way to forcibly end a command from within the command itself?

EDIT: Also, can the Cancel() method be used on a CommandGroup object? If so, I could work around this issue.

Within your command:


if(getGroup() != null) getGroup().cancel();
else cancel();

What about listening for the interrupt and setting a variable that will finish the command during the next iteration?

Will this cause issues if multiple commands are trying to end their parent CommandGroup?

Hmm. What does it mean to “listen” for an interrupt?

I have a background thread that checks for a certain button combination on the joystick being pressed. The Cancel() method will be called when that condition is true.

That’s polling, not an interrupt.

My mistake - I apologize for the error. What would an interrupt be then?

The hardware generates an electrical signal (called an interrupt) that causes the CPU to stop what it’s doing, save its “context”, jump to a new location in program memory, and start executing there. That location is called an ISR – interrupt service routine. When the ISR is done executing, it tells the CPU to go back to where it was when the interrupt occurred. The CPU uses the saved “context” to go back to where it was, as it was.

You can see there is no polling involved. That’s the advantage of interrupts.

The disadvantage is that interrupts can occur at any time – asynchronously – so you have to be careful when using them.