Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

it looks like it should be well known issue but I haven't found answer anywhere. Please tell me why the result (after clicking) of this:

Button["X", Print@1; Pause@1; Print@2;]

is different from result of this:

Print@1; Pause@1; Print@2;
share|improve this question

1 Answer

up vote 9 down vote accepted

The reason is because Button actions are calculated on a preemptive link, meaning they preempt any other evaluation, but are only allowed a certain amount of time to evaluate.

You can replicate the behavior of

Print@1; Pause@1; Print@2;

by adding the option Method->"Queued" to the Button arguments.

This ensures the actions are performed in the current queue and no time limit is enforced.

See the documentation under "Details and Options"

share|improve this answer
5  
A little more explanation to the OP: Most likely, 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. This is why 1 and 2 seems to appear simultaneously after a pause of 1 second. – Szabolcs Feb 13 at 20:16
@Szabolcs Thanks for the added color. – kale Feb 13 at 20:17
I've thought so but I missclicked and with "Queyed" get no difference :D – Kuba Feb 13 at 20:22

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.