Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's a perfect example of my problem:

Manipulate[Pause[n], {n, {1, 10}, ContinuousAction -> False}, SynchronousUpdating -> False]

It is illustrated by clicking 10 and then immediately clicking 1, that is, you shouldn't have to wait, it should stop pausing for ten seconds and begin pausing for only one second.

In general, Mathematica does not release a previous computation until the current one is finished. I need to override this in my application, which involves a computationally expensive function for only some values in the slider, and I want the computation to stop and restart when the mouse is dragged.

A Manipulate itself is not necessary for me, perhaps some trickery with Dynamics can achieve this? Also, would your solution work if the function that the slider drives is written with javalink?

share|improve this question
Nice question. Of the top of my head I can think of a way for the particular "pause" case, but I'm sure that serves no purpose for you. Hummm – Rojo Oct 15 '12 at 16:50
@NasserM.Abbasi it's possible to make a pause controlled by another variable, that ends when you set it – Rojo Oct 15 '12 at 22:37
up vote 3 down vote accepted

Ok, I still hope for a solution to the more general question on how to abort and relaunch a main link evaluation from the preemtpive link. But I'll offer a simple solution to the problem at hand. It just demonstrates how you can use a flag to stop your computation.

timings[t_] = PDF[NormalDistribution[1/2, 1/10]][t];

SetAttributes[myComputation, HoldFirst];
myComputation[x_] := Identity@With[{xval = x},
    If[x =!= xval, 
     Return[xval, myComputation]], {Round[timings[x]/0.05]}]; x

Dynamic[myComputation[x], SynchronousUpdating -> False]
Plot[timings[t], {t, 0, 1}]

or Slider[Dynamic[x], ContinuousAction -> False] if you prefer.

I assumed that your Pause was there to represent a particular computation and not a real pause. If that was not the case please say.


I'm writing this here because the comments are not enough, hope you don't mind! So here's a computation that takes a varying amount of time:

AbsoluteTiming[Total@Table[i^4, {i, 10}]]
{0.000062, 25333}

AbsoluteTiming[Total@Table[i^4, {i, 10000000}]]
{19.341138, 20000005000000333333333333333000000}

When I drag the slider all the way to the right and let go and then immediately drag it all the way back to the left I shouldn't have to wait the whole 19 seconds. But I don't see how your code fixes this....

SetAttributes[myComputation, HoldFirst];
myComputation[x_] := Identity@With[{xval = x},
   If[x =!= xval, Return[xval, myComputation]];
   Total@Table[i^4, {i, x}]]
{Slider[Dynamic[x], {10, 10000000}], Dynamic@x, 
 Dynamic[myComputation[x], SynchronousUpdating -> False]}
share|improve this answer
Ok, this looks good, can you use my original example with the function: f[x_] := Total@Table[i^4, {i, x}] – M.R. Oct 15 '12 at 22:04
@M.R. and what would x be, your example's n? In any case that example should get evaluated in no time – Rojo Oct 15 '12 at 22:35
@M.R. You should look at Sum -- it is likely better than Total@Table[...] for your application. – Mr.Wizard Oct 15 '12 at 22:53
@Mr.Wizard lol! For once, I was actually trying to have a SLOWER function to test this on... – M.R. Oct 16 '12 at 14:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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