1

I defined a function, solve, with a free variable n, which changes while solve is running. Therefore, evaluating

Dynamic[n]

solve[];

allows me to see the changes. But when I put solve into any sort of interface element like a button, I dont see n changing. Even if I evaluate

Dynamic[n]

bn = 0;
Dynamic[If[bn === 1, {bn = 0, solve[x]}]]

bn = 1;

I only see n change when the evaluation of solve is complete. What do I need to do to be able to observe changes to n as they happen?

Edit

With this, I see n change as evaluation progresses:

n = 0; solve[] :=  Do[n += 1, {1000000}]; Dynamic[n]; solve[]

With this, I only see n change at the end when it reaches 1000000:

bn = 0; Dynamic[If[bn === 1, {solve[], bn = 0}]]; bn = 1;
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
søren
  • 11
  • 1
  • 3
    Please post the code defining solve. Without this we don't have enough information to help you. – m_goldberg Feb 10 '13 at 02:57
  • n = 0;

    solve[] := Do[n += 1, {1000000}];

    Dynamic[n]

    solve[] <- This makes n changes along progress

    Dynamic[If[bn === 1, {solve[], bn = 0}]];

    bn = 1; <- here i see n only change at end to 2000000

    – søren Feb 10 '13 at 09:50
  • Please do not put requested example code in a comment. Add it to your original question as an edit. This time, because you are new to Mathematica.SE, I did it for you to show how it's done. – m_goldberg Feb 10 '13 at 15:17
  • We've had quite a few similar questions. For example, see this one – m_goldberg Feb 10 '13 at 15:25

1 Answers1

4

Since you did not show some code, I am here guessing what your problem can be from your mention of the use of a Button.

First, a direct example that works as is:

n = 0;
Dynamic[n]
solve[] := Module[{i},
  Do[n++; Pause[.21], {i, 10}]
  ]

Now, if you put this in a Button as is, you will only see the final result:

n = 0;
Dynamic[n]
Button["click me", Do[n++; Pause[.2], {i, 10}]]

However, to see n changes after you click the button, you need to set the Method option:

n = 0;
Dynamic[n]
Button["click me", Do[n++; Pause[.2], {i, 10}], Method -> "Queued"]

Now you will see n changes.

Nasser
  • 143,286
  • 11
  • 154
  • 359