In studying the last Basic Example in the documentation for StartScheduledTask
, namely
fun := With[{t = RandomInteger[{1, 3}]},
Print["Waiting ", t, " seconds."]; Pause[t]]
RunScheduledTask[fun; StartScheduledTask[$ScheduledTask], {1}];
RemoveScheduledTask[ScheduledTasks[]];
I first noted that nothing Print
s. That doesn't scare me too much; I would just guess that since the task is on another thread, it would have difficulty printing to the Notebook thread, and this might be a regression overlooked in recent versions (it doesn't print when evaluating directly in the documentation, either; MMA 9.0.1.0 on Mac OS X 10.9).
I decided to peek inside the task by making it assign something to a Dynamic
that's on display in the Notebook, so I wrote
$foo = 1;
Dynamic[$foo]
RemoveScheduledTask@ScheduledTasks[];
$foo = -1;
$fun[] :=
With[{t = RandomReal[]},
$foo = ("Waiting " <> ToString@N[t, 2] <> " seconds.");
Pause[t]];
RunScheduledTask[$fun[];
StartScheduledTask[$ScheduledTask], {1}];
When I run this, the value of $foo
is updated with the random number t
, but it seems to be after doing the Pause
. In other words, let's say t
has the following sequence of values
0.10000
0.90000
0.20000
Now, I can feel the difference between 0.1 seconds and 0.9 seconds and 0.2 seconds, and it feels like 0.1 is Displayed for 0.9 seconds, and the number 0.9 is displayed for 0.2 seconds, and so on. In other words, the display looks like
Waiting 0.10000 seconds (* this displays for 0.9 seconds *)
Waiting 0.90000 seconds (* this displays for 0.2 seconds *)
etc.
What's going on here?
Print[]
is going to the Messages window $\endgroup$