3

I want to monitor a long calculation that is triggered by the release of a Slider. While one can indicate running calculations when the main evaluation loop is controlled by a queued Button, the same cannot be done with more direct Dynamic approaches. A bit of trial and error actually confirms that only Button and not any of the other controls (Slider, Locator, even EventHandler) can be evaluated non-preemptively.

This works as expected, displaying a ProgressIndicator while the long calculation is performing:

long[] := Module[{a = .3}, Do[a = 3.5 a (1 - a), {i, n}]];
{active, x} = {False, 1};
{Slider[Dynamic@x, {0, 1}], 
 Button["Update", (active = True; long[]; active = False;), Method -> "Queued"]}
Dynamic@{If[active, ProgressIndicator[i, {0, 10^6}], "..."], active}

Mathematica graphics

The same cannot be done using a Slider. The update command was put in the second argument of Dynamic, more precisely in the slot which is evaluated when the controller is released. As you can see, it still evaluates long (the string is printed) but it does not switch active and consequently it does not display the ProgressIndicator.

long[] := Module[{a = .3}, Do[a = 3.5 a (1 - a), {i, 10^6}]];
{active, x} = {False, 1};
Slider[Dynamic[x, {(x = #) &, (active = True; Print["evaluating..."]; long[]; 
     active = False) &}], {0, 1}]
Dynamic@{If[active, ProgressIndicator[i, {0, 10^6}], "..."], active}

Mathematica graphics

No amount of TrackedSymbols :> {...}, ContinuousEvaluation -> False or SynchronousUpdating -> False helped.

Is there any way to force $f_{end}$ in Dynamic[$x$, {$f$, $f_{end}$}] to be evaluated in a queued fashion, not preempting (its own) previous commands?

As a sidenote: It would be interesting to look into the implementation of Button as it definitely stands on something different than EventHandler. Does anyone has some insight on this matter?

István Zachar
  • 47,032
  • 20
  • 143
  • 291
  • Are you aware of this? It looks to me like an authoritative "not possible" (yet?) to what you want to achieve, or am I missunderstanding your question? – Albert Retey Mar 28 '13 at 21:36
  • @Albert Thanks! I did perform a search but missed that one. Seems to be relevant and thus the answer to my question is no. – István Zachar Mar 28 '13 at 22:03
  • would you consider this question to be a duplicate to be removed? – Albert Retey Mar 28 '13 at 22:10
  • @Albert Yes, I guess it is. I was thinking overnight whether there is any subtle difference offering an escape, but no, there is no solution to this issue yet. Would be nice to know more about why the Method option was not implemented for dynamic controllers other than Button. – István Zachar Mar 29 '13 at 07:57
  • @rm -rf I'm not sure I did the appropriate thing here: should I simply delete my Q or is it fine to be closed as duplicate? In the latter case, should I refer to this from the master Q (in a comment) as it elaborates on the same matter and might be useful for others? – István Zachar Mar 29 '13 at 08:01
  • István, it's fine for the Q to be closed as a dupe, as it will help in Google searches. This Q is also automatically linked from the other one (see under "Linked" in the right sidebar of Joel's question). However, unregistered users and 1 rep users will not be able to directly view this question since they'll automatically be redirected to Joel's. If you want to leave a link to this one in the comments for those people, then use this link (note the ?noredirect=1 in the URL) – rm -rf Mar 29 '13 at 18:50

0 Answers0