1
$\begingroup$

I'm trying to abort a While cycle, with a dialog box. I've tried the following:

res = 0;
res2 = "";
CreateDialog[{TextCell["Do you want to continue calculations?"], 
              Button["Proceed", DialogReturn[res2 = True]], 
              Button["Cancel", DialogReturn[res2 = Abort[]]]}];

While[True , res++; If[res > 10, res = -1]; res2];

However, this does not work. It's important that the Dialog window may be left opened while the While continues to 'work'.

I've tried this but I was unable to adapt that solution to my problem...

Any help would be appreciated.

$\endgroup$
2
  • $\begingroup$ I see you have not accepted any of your questions' anwsers. Aren't they useful? Please take a tour to see how it works. $\endgroup$
    – Kuba
    Commented Jan 19, 2015 at 10:46
  • $\begingroup$ @Kuba thanks and sorry for the delay $\endgroup$ Commented Jan 19, 2015 at 14:11

1 Answer 1

1
$\begingroup$

If you use SetDelayed then you loop will call Abort. With Set (=) res2 has not value at all. You can check it evaluating:

a =. ;
a = Abort[]

(*new cell*)

a
a

So:

res = 0;
res2 = "";

CreateDialog[{TextCell["Do you want to continue calculations?"], 
              Button["Proceed", DialogReturn[res2 = True]], 
              Button["Cancel", DialogReturn[res2 := Abort[]]]}];

While[True, res++; If[res > 10, res = -1]; res2];

will do what you need.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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