Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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[{},
  EventHandler[
   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.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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"; \
Slider[]
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"; \
Slider[]
DynamicModule[{trigger = 0},
 DynamicWrapper[
  EventHandler[Framed["Benjamin Button"], "MouseDown" :> (++trigger)],
  If[trigger =!= 0,
   Refresh[Pause[3]; Print@"If you couldn't, it was preemptive", 
    None]], SynchronousUpdating -> False
  ]
 ]
share|improve this answer
1  
+1 This was extremely useful, thanks Rojo! See application here. I'm not sure I share your concern about the uglyness :) –  István Zachar Mar 30 '13 at 10:56
1  
Forgive me for huge delay, I forgot I hadn't accepted Your answer :/ –  Kuba Jun 3 '13 at 19:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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