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.

Possible Duplicate:
Why do buttons with ChoiceDialog freeze the front end?
Why is SystemDialogInput failing to return a save file path?
Evaluation inside Button stops after some seconds

I am trying to use the code from:

Make a button that evaluates a function over and over

to make a button evaluate some code and plot the results, but when the code takes more than a few seconds it does not plot the results.

Here is a simple example that illustrates the problem I am having

For short time periods it works fine:

    func := (Pause[1]; Plot[Sin[x], {x, 0, 5}]);
    DynamicModule[{result = Null},  Column[{Button["Press", result = func], Dynamic[result]}]]

But if I change the pause to be 10 seconds it doesn't show the plot

    func := (Pause[10]; Plot[Sin[x], {x, 0, 5}]);
    DynamicModule[{result = Null},  Column[{Button["Press", result = func], Dynamic[result]}]]
share|improve this question
add comment

marked as duplicate by Sjoerd C. de Vries Sep 4 '12 at 20:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Try

func := (Pause[10]; Plot[Sin[x], {x, 0, 5}]);
DynamicModule[{result = Null}, 
Column[{Button["Press", result = func, Method -> "Queued"], 
Dynamic[result]}]]

From MMA documentation:

By default, button functions are evaluated on a preemptive link, which times out after 5 seconds:

Use Method->"Queued" to evaluate button functions, which never times out.

share|improve this answer
    
Perfect thank you! –  David Slater Sep 4 '12 at 20:03
add comment

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