I would like to run a time consuming function:


and be able to change maxseconds during running (increasing or decreasing max time) using a slider. Ideally when i decrease maxseconds below the time it has already consume it will stop.

I could implement such a functionality inside f but i would have to do this for all f i want to create , so this way is out of the question.

Thanks in advance,


  • $\begingroup$ This is a tough one. Basically it boils down to the fact that once a long calculation is started, you cannot interrupt it programmatically from the outside (i.e. not from the inside as in case of e.g. EventMonitor:>If[aborted, Abort[]]) with any Abort[], even using a background scheduled task to monitor elapsed time. $\endgroup$ Nov 29, 2013 at 12:47
  • $\begingroup$ I understand your point. This is why I opened the bounty - in case there is a way of generalizing TimeConstrained maybe with some low level Java programming? $\endgroup$
    – tchronis
    Nov 29, 2013 at 14:24
  • 3
    $\begingroup$ Perhaps "EvaluatorAbort" could be used. $\endgroup$
    – Michael E2
    Nov 29, 2013 at 15:44
  • $\begingroup$ Thanks @MichaelE2 for the input! $\endgroup$ Nov 29, 2013 at 18:25
  • 2
    $\begingroup$ Another completely different approach would be to abuse one of the parallel kernels to run the computation in a detached thread. After all, the parallel kernels are just simple kernels you can use for calculation. In this way you could work in the front end while your f[x] is calculated in the background and you can stop the calculation at any time. $\endgroup$
    – halirutan
    Nov 30, 2013 at 2:55

1 Answer 1


Thanks to Michael E2's comment, the following approach is successful. The method sets up a scheduled task that (at certain resolution res) monitors the elapsed $time and compares it to the dynamic $max. If $time is more than allowed by $max, it calls the front-end "EvaluatorAbort".

Attributes[dynamicTimeConstrained] = {HoldAll};
dynamicTimeConstrained[expr_] := Module[{task, res = .1},
   $time = 0;
   task = RunScheduledTask[(
      $time = $time + res;
      If[$time >= $max,
           "Aborted at "<>ToString@$time<>".", "Output"];
      ), {res, Infinity}];
   res = expr;

$time = 0;
$max = 3;

(* set up slider to dynamically manipulate $max *)
Slider[Dynamic@$max, {0.0001, 10}, Appearance -> "Labeled"]

(* simulate a long calculation by Pause *)
dynamicTimeConstrained[Do[Pause@1; Print[i];, {i, 5}]; 111]

Notice that the scheduled task is aware of the global value changes to both $time and $max, so the method works. May need a bit of fine tuning on the correct time-resolution as too fine a resolution might cause delayed abortion.

Here the default allotted time (3 sec) is extended, so the calculation terminates normally without abort:

enter image description here

Here the default time (3 sec again) is decreased and the job aborts as expected:

enter image description here

  • $\begingroup$ Great job! Glad the comment helped. :) $\endgroup$
    – Michael E2
    Nov 29, 2013 at 18:31
  • $\begingroup$ @tchronis Thanks for the accept and the bounty! I hope this method can be used in realistic situations as well. $\endgroup$ Dec 13, 2013 at 7:56

Your Answer

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

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