1
$\begingroup$

I am creating an interface which needs to repeat an operation every few seconds. The operation itself is expensive but faster than the update interval of the interface.

I would like to have some visual feedback to know every time the operation starts and ends but I have not been able to find a solution that works well.

This is what I have now:

expensive[k_] := (Pause[2]; k + 1)

DynamicModule[{k = 0, status = "not calculating"},
 {
  Dynamic[
   Refresh[status = "calculating";
    k = expensive[k];
    status = "not calculating",
    UpdateInterval -> 5, TrackedSymbols :> {}]],

  Dynamic[{status, k}, TrackedSymbols :> {status}]
  }
 ]

Does anyone know how why it is not working and how to fix it?

This question is related to Showing "updating..." message while Manipulate is re-evaluating but the refresh is triggered by an UpdateInterval instead of a button.

$\endgroup$

1 Answer 1

1
$\begingroup$

Each Dynamic is using preemptive link and it is theoretically possible to do what you need. However, there are no tools to manage evaluation precisely (one could try with FinishDynamic[]). At the moment you are using one link which is busy so it can't update second Dynamic.

You can't count on specific order of tasks but you can send one to main link by using SynchronousUpdating -> False:

expensive[k_] := (Pause[2]; k + 1)

DynamicModule[{k = 0, status = "not calculating"}, 
  {Dynamic[
    Refresh[
      status = "calculating"; 
      k = expensive[k];
      status = "not calculating"
      , 
      UpdateInterval -> 5, 
      TrackedSymbols :> {}
    ]
    , 
    SynchronousUpdating -> False
  ], 
  Dynamic[{status, k}, TrackedSymbols :> {status}]}
]

p.s. are you sure you don't want to use ScheduledTasks?

$\endgroup$
1
  • $\begingroup$ Thank you Kuba. This works! For now I manage evaluation just with a UpdateInterval->If[autoUpdate, 5, Infinity]. Maybe down the road I switch to a ScheduledTask, but I generally try to avoid those. I once tried to use it for a once per day task and it was not reliable at all. Maybe for tasks with a shorter period it works better. $\endgroup$ Nov 16, 2015 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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