2
$\begingroup$

If you run the following code you will notice that the evaluation continues in the following cells.

I want to abort evolution if CancelButton is clicked.

I tried to use FrontEndTokenExecute["EvaluatorAbort"] but it doesn't help:

DynamicModule[{yyyy = 2015, mm = 6, dd = 20}, DialogInput[Grid[{
     {"Year:", InputField[Dynamic[yyyy], Number]},
     {"Month:", InputField[Dynamic[mm], Number]},
     {"Day:", InputField[Dynamic[dd], Number]},
     {CancelButton[
       DialogReturn[FrontEndTokenExecute["EvaluatorAbort"]]], 
      DefaultButton[DialogReturn[{Year = yyyy, Month = mm, Day = dd}]]}
     }, Spacings -> {1, Automatic}, Alignment -> Left]]];

RandomReal[]

Any idea?

Thanks

$\endgroup$
1
$\begingroup$

So far and with help of Kuba's links I got the following solution:

DynamicModule[{yyyy = 2015, mm = 6, dd = 20, cancel = False}, 
  DialogInput[Grid[{
     {"Year:", InputField[Dynamic[yyyy], Number]},
     {"Month:", InputField[Dynamic[mm], Number]},
     {"Day:", InputField[Dynamic[dd], Number]},
     {CancelButton[DialogReturn[cancel = True]], 
      DefaultButton[
       DialogReturn[{Year = yyyy, Month = mm, Day = dd}]]}}, 
    Spacings -> {1, Automatic}, Alignment -> Left]];
  If[cancel, FrontEndTokenExecute["EvaluatorAbort"]]];
$\endgroup$
  • $\begingroup$ It doesn't work on my machine (Mathematica 10.0.2.0,Windows 7). That is to say : RandomReal[]is still evaluated when I push the button "Cancel". (I must leave. Be back in 11 hours) $\endgroup$ – andre314 Jan 13 '16 at 7:18
  • 1
    $\begingroup$ @andre you probably mean that you have RandomReal[] in the same cell that DynamicModule is. Then that's correct. $\endgroup$ – Kuba Jan 13 '16 at 7:42
  • $\begingroup$ @andre and if you want to abort what is scheduled in current cell I'm afraid you have to use my stupid $AssertFunction from linked thread. $\endgroup$ – Kuba Jan 13 '16 at 7:46
  • $\begingroup$ @Kuba You are certainly right. I can reproduce what you say. Nevertheless. I thought that RandomReal[] was in a separate cell this morning. I have done a mistake somewhere else probably, but i can't reproduce it. $\endgroup$ – andre314 Jan 13 '16 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.