2

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

Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78

1 Answers1

1

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"]]];
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
  • 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) – andre314 Jan 13 '16 at 07:18
  • 1
    @andre you probably mean that you have RandomReal[] in the same cell that DynamicModule is. Then that's correct. – Kuba Jan 13 '16 at 07:42
  • @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. – Kuba Jan 13 '16 at 07:46
  • @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. – andre314 Jan 13 '16 at 18:36