Classes | Scheduling

Task : PauseStream : Stream : AbstractFunction : Object

a pauseable process
Source: Stream.sc


Task is a pauseable process. It is implemented by wrapping a PauseStream around a Routine. Most of its methods (start, stop, reset) are inherited from PauseStream.

The purpose of a Task is to separate a Routine's state of execution from its running state (that is, its state of being scheduled on a clock or not, or paused in a CondVar or not). Use Task if you expect the process to need to start, stop or resume multiple times while maintaining the execution flow. (This means that Tasks are not 100% interchangeable with Routines -- for many uses, Tasks should be preferred over Routines.)

Note that stopping a task and restarting it quickly may yield surprising results (see example below), but this is necessary to prevent tasks from becoming unstable if they are started and/or stopped in rapid succession. (Routines do allow a quick stop-reset-play cycle, but they have no mechanism to prevent timing from being broken in this case, i.e. Routine is more brittle here.) If you need to start and stop quickly while maintaining timing, a better approach would be to swap the child Routine over to a new instance of PauseStream.

Class Methods

Task.new(func, clock)



A Function to be evaluated.


A Clock in which to play the Routine. If you do not provide a Clock the default is an instance of TempoClock. Remember that methods which call Cocoa primitives (i.e. GUI functions) must be played in AppClock.

Inherited class methods

Instance Methods

.play(argClock, doReset: false, quant)

From superclass: PauseStream



(optional) Override the clock assigned in Task.new.


If true, the task will start over from the beginning. Default is false (task will resume where it was when it was last stopped).


See the Quant helpfile.

Other control methods

.start(argClock, quant)

From superclass: PauseStream

Restart the task from the beginning.

.resume(argClock, quant)

From superclass: PauseStream

Resume the task where it left off.


From superclass: PauseStream

Stop playing now.


From superclass: PauseStream

Stop playing now. (Pause and stop have the same implementation.)


From superclass: PauseStream

Set the stream to restart from the beginning the next time it's played.

.reschedule(argClock, quant)

From superclass: PauseStream

Switch the Task to a different clock, or a different time, without stopping. See Routine: -reschedule for complete documentation.

NOTE: If you want to reschedule a Task from within the Task itself, thisThread.reschedule(...) will not work, because thisThread refers to the Routine under control of the Task, not to the Task itself (whereas a Routine is playing on the clock directly). You must write thisThread.threadPlayer.reschedule(...) instead.


Other objects might need to be aware of changes in the state of a task. The following notifications are broadcast to dependents registered with the Task object.

Inherited instance methods

Undocumented instance methods



What happens if you stop and start the task too quickly?

Based on the forked thread, you would expect the second "go" line of output to occur 0.2 seconds after the first, but in fact it happens two seconds later (the same amount of time the task waits between iterations). This is because the task must not schedule itself on the clock more than once. When the task is stopped, it remains scheduled until it wakes up again (based on its wait time). If, during this interval, the task were restarted, there would be two references to the task in the scheduler queue -- a situation that is irrecoverable short of stopping everything with command-period.

For the above case, you can get completely stable timing by manually wrapping the Routine in a PauseStream. Note that start implicitly resets the routine to the beginning; using play instead only alters the timing, without interrupting the routine's flow.