Hi all. I have created a window with a progress bar and a "Cancel" button in it. The intent is to allow the user to stop program execution. I'm getting an error when I press cancel though, and do not know how to resolve it. A simplified version of my script is below.
Names Default to Here(1);
//Define progress window
pwin = NewWindow("Progress",
SB_L = SpacerBox(Size(0, 15), Color("Green")),
ButtonBox("Cancel",
pwin << Close Window;
Stop();
)
);
//Sample Loop
For(i=1, i<=10, i++,
SB_L << Size(i*10, 15); //update the progress bar
Wait(1); //rest of loop
);If I press Cancel while the loop is running, I get the following error:
deleted object reference: SB_L << Size(i * 10, 15) in access or evaluation of 'Glue' , pwin = New Window("Progress",
SB_L = Spacer Box(Size(0, 15), Color("Green")),
Button Box("Cancel",
pwin << Close Window;
Stop();
)
); /*###*/For(i = 1, i <= 10, i++,
SB_L << Size(i * 10, 15);
Wait(1);
) /*###*/;When the cancel button is pressed, my expectation is that the progress window should close, and then execution should stop. But it appears to continue on for at least one iteration, and fails at the SB_L << Size() line in the For loop. It fails because the progress window has closed and SB_L has been deleted, but I'm confused why this line is getting executed if the Stop() command was issued?
Am I correct that program execution is continuing even after my Stop() command? How can I prevent the execution of the SB_L << Size() command after the Cancel button has been pressed?
Details:
JMP Pro 15.2.1
Mac OS Big Sur v11.6
Thanks.
Hi @nikles . You may want to check out this recent post from @Craige_Hales , Progress Bar with Cancel Button .
Thanks Ryan (and Craige). This was one option I'm definitely considering, but it requires sending a flag to an if clause in the main script body. My wish is to just stop program execution using the Stop() command in the Cancel button. Maybe I can reframe my question: why doesn't my Stop() command seem to stop execution?
Just noticed something else interesting. If I comment out the "pwin << Close Window()" command in the Cancel button script, the cancel button no longer stops execution. It seems the Stop() command is not able to stop the For Loop when it is executed from a button. Cancel button was able to stop execution before only because removing the window created an error.
wait( n ) is a necessary evil in JMP, to be avoided where possible. During a wait, the script that calls wait is paused while the OS can do things, like recognizing a click or making an open window visible. Weird things can happen, because you can use the click to start another script (your example) that touches the environment of the first script. In your example, stop() is stopping the second script. It can also set a global flag. If your project is big enough to worry about keeping track of global variables, you can also use name spaces.
So what is the weird thing that can happen? Mostly windows being closed, or displayboxes being deleted. In particular, imagine a click function that has a script that uses wait(1), and you click the button again, half a second later. A second copy of the script is running inside the first. The original wait(1) won't return until the second script finishes. If the second script closes the window the button lives in, what environment is around the original script? (not the same one as before the wait!) Don't build scripts that require wait without a good reason; most of the time displaybox<<