I'm experimenting with Inversion-of-Control (callback) programming patterns, and I would like a way to generate streams of data at specified real-time intervals. I came up with the illustrated way to use Animate (or Manipulate) to do it, and that's fine for experiments with the notebook front end, but I would like to have pure-code solutions I could put in a .m package. As always, I will be grateful for any hints, advice, and solutions!
1 Answer
You could use RunScheduledTask
or its relatives for this. For example, to append a random integer to catch
once every two seconds you could do something like
catch = {};
task = RunScheduledTask[AppendTo[catch, RandomInteger[10]], 2];
You could also use CreateScheduledTask
which is similar to RunScheduledTask
except that the task won't be started automatically after it's been created. You'll have to use StartScheduledTask
to start it manually.
To start and stop the scheduled task you can use StartScheduledTask[task]
and StopScheduledTask[task]
, respectively, and RemoveScheduledTask[task]
will remove the scheduled task once you're done with it.
Additional information about these functions is available in the Documentation Center.
RunScheduledTask
and related functions? $\endgroup$