I would like to construct a GUI using just the Mathematica language that allows me to start and stop a calculation interactively with the press of buttons. I have set up the following to show what I wish to do, although you will notice that I can start the calculation, but I can't stop it.
Pressing Start will begin a count-up of num and dynamic update of the field to show num. I have a limit of 20 set so that the loop does not go on forever. I wish to be able to hit Stop along the way and halt the loop. Obviously, I need help figuring out a good way to do this through Dynamic, Monitor or some other construct.
DIAG2 := CreateDialog[
Module[{buttonpar, startbutton, stopbutton, startcalc, stopcalc,
labels, fields, num, numlim, calc},
numlim = 20;
calc = False;
labels = {"num"};
fields = {InputField[Dynamic[num], Number, Enabled -> False]};
startcalc[] := (
num = 0;
calc = True;
Print["START"];
While[calc && (num <= numlim), Pause[1]; num = num + 1;
num = num++]
);
stopcalc[] := (
calc = False;
Print["STOP"];
);
buttonpar = {ImageSize -> All,
BaseStyle -> {"Evaluate", 12, Bold}};
startbutton =
Button["START", startcalc[], Evaluate@buttonpar,
Method -> "Queued", Enabled -> True];
stopbutton =
Button["STOP", stopcalc[], Evaluate@buttonpar,
Method -> "Queued", Enabled -> True];
Panel[TableForm[{startbutton, stopbutton, labels, fields}]]
],
Modal -> False, WindowTitle -> "START/STOP TEST"
];
I run it with this:
DIAG2;
Thank you for your attention and help with this.
Ben
==================== UPDATE! ====================
Thanks to Gustavo's answer, I have modified the code to the following, which does what I had originally wanted. Now I have also include a Reset button as well as using a dynamic variable en for the Enabled parameter. This sets the Reset and Start buttons as executable at the beginning, but then they are greyed out after Start. Stop does the opposite.
I would still appreciate any further advice or comments on executing this GUI, perhaps with improvements, listing any potential problems, etc. Thank you!
DIAG2 := CreateDialog[
Module[
{buttonpar, resetbutton, startbutton, stopbutton, resetcalc,
startcalc, stopcalc, labels, fields, num, numlim, en},
numlim = 100;
num = 0;
en = True;
labels = {"num"};
fields = {InputField[Dynamic[num], Number, Enabled -> False]};
resetcalc[] := (
num = 0;
Print["RESET"];
);
startcalc[] := (
Print["START"];
en = False;
While[num <= numlim, Pause[1]; num = num + 1; num = num++];
);
stopcalc[] := (
FrontEndTokenExecute["EvaluatorAbort"];
en = True;
Print["STOP"];
);
buttonpar = {ImageSize -> All,
BaseStyle -> {"Evaluate", 12, Bold}};
resetbutton[en_] :=
Button["RESET", resetcalc[], Evaluate@buttonpar,
Method -> "Queued", Enabled -> en];
startbutton[en_] :=
Button["START", startcalc[], Evaluate@buttonpar,
Method -> "Queued", Enabled -> en];
stopbutton[en_] :=
Button["STOP", stopcalc[], Evaluate@buttonpar,
Method -> "Preemptive", Enabled -> ! en];
Panel[
TableForm[{resetbutton[Dynamic[en]], startbutton[Dynamic[en]],
stopbutton[Dynamic[en]], labels, fields}]]
],
Modal -> False, WindowTitle -> "START/STOP TEST"
];
Best Regards, Ben