13

Pressing the button below will freeze my front end. An unpainted white dialog box comes up, then it completely freezes, and I need to forcibly kill the front end. CPU usage stays at zero during this.

Button["Ask", If[ChoiceDialog["OK?"], Print["OK!"]]]

Why does this happen?

What is the proper way to ask for confirmation before an action?


Answers:

My first question has been answered below and in the comment by @JohnFultz: use Button[..., Method -> "Queued"]. But this will make a button which doesn't work while evaluations are ongoing.

I need a button which works even during evaluations. I came up with the following solution:

Button["Ask", 
 CreateDialog[
  Column[{"OK?", 
    ChoiceButtons[{"OK"}, {Print["OK!"]; DialogReturn[]}]}]]]

CreateDialog returns immediately without waiting for an input, so the front end is not blocked. The functionality is moved completely into the dialog's button.

You can test that the button works and reacts immediately even while Pause[10] is evaluating.


Quoting the two most enlightening comments:

The "Preemptive" method interrupts queued (e.g., Shift+Enter) evaluations to do its work and cannot itself be interrupted. I.e., you can't preempt a preemptive calculation. ChoiceDialog is putting up an interface which independently needs the attention of the Mathematica kernel, but can't get it because Button has the kernel's sole attention - John Fultz

 

Button[..., Method->"Preemptive"] (the default Method value) and InputString[] are effectively exactly the opposite of each other. Button[..., Method->"Preemptive"] will pause the user interface (causing it to stop responding to events) until the computation is complete. InputString[] will pause the computation until the user has input some value. When you combine these two the user interface is waiting for the computation to complete while at the same time the computation is waiting for user interface input. - ragfield

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 1
    It's not a bug. Please see this question. – Szabolcs May 08 '12 at 19:39
  • 1
    Please also see here – Szabolcs May 08 '12 at 19:40
  • 1
    I flagged those two questions to be moved here. I propose we merge all three (I volunteer to do the editing). – Szabolcs May 08 '12 at 19:43
  • Please merge and edit as you wish @Szabolcs. Funny, I did a search of the archives and tags, and the mma beta question did not come up. Shall we tag that one with something more descriptive than "untagged"? – JxB May 08 '12 at 19:54
  • Of course! Let's just wait for the other question to be migrated as well :-) The other question is my own, and I also didn't find this when searching SO before I asked it. – Szabolcs May 08 '12 at 20:10
  • My vague guess is this has to do with evaluating things on the main or preemptive link, but I'm not sure ... – Szabolcs Jan 16 '12 at 19:15
  • @NasserM.Abbasi Thanks, ragfield's description there is very instructive. – Szabolcs Jan 17 '12 at 01:03
  • I just bumped into this, and it hasn't got an accepted answer. If you are expecting more, I think I could write up something, but what exactly is the question left to answer? – Rojo Feb 22 '13 at 14:19
  • @Rojo I don't remember why I didn't accept, now I did. The most valuable are the comments by ragfield and JF. – Szabolcs Feb 22 '13 at 16:12

3 Answers3

13

Try Method-> "Queued". This works for me, although I cannot say just why.

Button["Ask", If[ChoiceDialog["OK?"], Print["OK!"]], Method -> "Queued"]

Here's the explanation from the docs:

enter image description here

DavidC
  • 16,724
  • 1
  • 42
  • 94
  • 7
    The "Preemptive" method interrupts queued (e.g., Shift+Enter) evaluations to do its work and cannot itself be interrupted. I.e., you can't preempt a preemptive calculation. ChoiceDialog is putting up an interface which independently needs the attention of the Mathematica kernel, but can't get it because Button has the kernel's sole attention. – John Fultz Jan 16 '12 at 22:10
  • @John Fultz Thanks. That now makes sense. – DavidC Jan 16 '12 at 22:23
  • I'm also wondering if there's a way to "unfreeze" the front end if this happens ... – Szabolcs Jan 17 '12 at 00:39
  • @Szabolcs Every time the front end froze and I called up Force Quit (on my Mac), a dialogue box from mma popped up asking me if I wanted to abort the evaluation. Then aborting worked. This may have been a mere coincidence but it happened 5 or 6 times. – DavidC Jan 17 '12 at 00:44
8

Try the following two alternatives, pressing the button while the Do loop is waiting.

Button["Click Here", Print[10!], Method -> "Preemptive"]
Do[Print[x]; Pause[5], {2}]
(*
 -> x
 -> 3628800
 -> x
*)

and

Button["Click Here", Print[10!], Method -> "Queued"]
Do[Print[x]; Pause[5], {2}]
(*
 -> x
 -> x
 -> 3628800
*)

Some Explanation:

You can see that in the first case, the Do loop is interrupted for processing the Button procedure, but in the second case it is just queued and the Button processing is postponed until the Do loop ends.

In the case of the "Preemptive" method, Mma wants to assure that you are not stealing the CPU forever and impeding the Do loop finalization, so it gives you a certain time slice to process the Button procedure. If you yield control to human input (as in InputString[]), that time slice expires and thus the results you are getting.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
7

For this, you need to use Method->"Queued"

Button["hi", InputString[], Method -> "Queued"]
Nasser
  • 143,286
  • 11
  • 154
  • 359
  • Thanks! Interesting. I can't say I know enough of the inner-workings of Mathematica to understand why the hang-up occurs, although I just went into Documentation Center and saw that it crops up under "Possible Issues". –  Jul 20 '11 at 20:18
  • 11
    Button[..., Method->"Preemptive"] (the default Method value) and InputString[] are effectively exactly the opposite of each other. Button[..., Method->"Preemptive"] will pause the user interface (causing it to stop responding to events) until the computation is complete. InputString[] will pause the computation until the user has input some value. When you combine these two the user interface is waiting for the computation to complete while at the same time the computation is waiting for user interface input. – ragfield Jul 21 '11 at 01:35