RunScheduledTask

RunScheduledTask[expr]
schedules and starts a task that will repeatedly evaluate expr once per second.

RunScheduledTask[expr, time]
schedules and starts a task that will repeatedly evaluate expr every time seconds.

RunScheduledTask[expr, {time}]
schedules and starts a task that will evaluate expr once after time seconds.

RunScheduledTask[expr, {time, count}]
schedules and starts a task that will try evaluating expr once every time seconds up to count times.

RunScheduledTask[expr, timespec, start]
schedules a task that will automatically start at start time.

DetailsDetails

  • RunScheduledTask returns a ScheduledTaskObject expression representing the started task.
  • RunScheduledTask[x] is effectively equivalent to StartScheduledTask[CreateScheduledTask[x]].
  • The start is taken to be the absolute time when the task is activated.
  • The first evaluation of expr will occur time seconds after it is activated.
  • If two tasks are scheduled to evaluate at the same time, the one added later will execute first.
  • A scheduled task does not require a notebook front end to work.
  • By default, if the time necessary to evaluate expr is larger than time, the next pending evaluation is skipped.

ExamplesExamplesopen allclose all

Basic Examples (11)Basic Examples (11)

The timer fires off at the end of the specified three-second interval:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input

Perform an action the specified number of times:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input

Create a task that will be activated 5 seconds from the current time and will print 1 second later:

In[1]:=
Click for copyable input
Out[1]=

Specify a date to activate the task:

In[1]:=
Click for copyable input
Out[1]=

Front end versus kernel clocks:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=
Out[2]=
In[3]:=
Click for copyable input

Create a task with random wait periods:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input
In[4]:=
Click for copyable input

Create a task that restarts evaluation as soon as it finishes:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input

Create a task that runs only once and removes itself after finishing:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Create a task that removes itself once a condition is met:

In[1]:=
Click for copyable input

Create a task that removes itself when an external variable is changed:

In[1]:=
Click for copyable input

Cause the task to be stopped and removed:

In[2]:=
Click for copyable input
Out[2]=

Control evaluation in the scheduled task by changing an external variable:

In[1]:=
Click for copyable input

Pause the evaluation without stopping the task:

In[2]:=
Click for copyable input
Out[2]=

Re-enable the evaluation:

In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
New in 8
New to Mathematica? Find your learning path »
Have a question? Ask support »