This question is related to Evaluation order of Button's action

Concusion was, Button[]'s actions are evaluated in preemptive link so "the code that needs to be run for the new cells to be created and formatted needs to wait until the preemptive evaluation has finished". To deal with it we only have to add Method -> "Queued" for Button.

I've thougth that in case:

Button[ "X", x = 3; Pause @ 1; y = 4]
Dynamic @ x
Dynamic @ y

where cells for x and y are created before pushing Button I don't have to use Method -> "Queued". However it seems I have to because Pause is again evaluated at the first place.

This is not a big problem since I know how to deal with it but I like to know why something works not the way I thought it would.

There is something more: What if I'm working with customized button based on EventHandler, let's take basic case:

SetAttributes[ GButton, HoldRest]
GButton[ob_, proc_] := DynamicModule[{},
   Framed[ob, Alignment -> Center],    
   {"MouseDown" :> (proc)}, PassEventsUp -> False, 
  PassEventsDown -> False]];

This kind of button works as a standard Button but how can I add anything that works as Method -> "Queued" in Button? I need something opposite to PreemptProtect function.


1 Answer 1


Check what The Futz just said. However, you can go the ugly workaround way. Not recommended, since I don't think you have any guarantees that your code will be evaluated exactly once only when you click the button, but up to you.

Try this

Print@"I dare you to move the slider after pressing the button"; \
EventHandler[Framed["Benjamin Button"], 
 "MouseDown" :> (Pause[3]; 
   Print@"If you couldn't, it was preemptive")]

and compare with this

Print@"I dare you to move the slider after pressing the button"; \
DynamicModule[{trigger = 0},
  EventHandler[Framed["Benjamin Button"], "MouseDown" :> (++trigger)],
  If[trigger =!= 0,
   Refresh[Pause[3]; Print@"If you couldn't, it was preemptive", 
    None]], SynchronousUpdating -> False
  • 1
    $\begingroup$ +1 This was extremely useful, thanks Rojo! See application here. I'm not sure I share your concern about the uglyness :) $\endgroup$ Mar 30, 2013 at 10:56

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.