({{, $ScheduledTask
returns within a scheduled task the (RefLink[ScheduledTaskObject, paclet:ref/ScheduledTaskObject]) or (RefLink[CloudObject, paclet:ref/CloudObject]) for the task.}})

OpenersNotesSection(Spacer1)Details

If it is evaluated not within a scheduled task, $ScheduledTask gives (RefLink[None, paclet:ref/None]).

$ScheduledTask is used to refer to the task within a task evaluation, e.g. to learn the parameters or stop the task.

OpenersPrimaryExamplesSection(Spacer1)Examples  (8)

OpenersExampleSection0(Spacer1)Basic Examples  (8)

Print the running scheduled task object:

RunScheduledTask[Print[$ScheduledTask]]

Print its time specification:

RunScheduledTask[Print[$ScheduledTask[[3]]]]

RemoveScheduledTask[ScheduledTasks[]];

Remove the scheduled task automatically when it is not needed:

RunScheduledTask[x=DateString[]];

ScheduledTasks[]

Dynamic[x]

RunScheduledTask[x=Graphics[{Red,Disk[]}];RemoveScheduledTask[$ScheduledTask],2];

ScheduledTasks[]

RemoveScheduledTask[ScheduledTasks[]];

Create a task with random wait periods:

time:=RandomInteger[{1,3}]

fun[t_]:=Print["Waiting ",t," seconds."]

RunScheduledTask[With[{t=time},ResetScheduledTask[$ScheduledTask,{t}];fun[t];StartScheduledTask[$ScheduledTask]],{1}];

RemoveScheduledTask[ScheduledTasks[]];

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

fun:=With[{t=RandomInteger[{1,3}]},Print["Waiting ",t," seconds."];Pause[t]]

RunScheduledTask[fun;StartScheduledTask[$ScheduledTask],{1}];

RemoveScheduledTask[ScheduledTasks[]];

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

RunScheduledTask[Print[RandomReal[]];RemoveScheduledTask[$ScheduledTask],0];

ScheduledTasks[]

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

RunScheduledTask[With[{x=RandomReal[]},Print[x];If[x>0.6,RemoveScheduledTask[$ScheduledTask]]],1];

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

x=0;RunScheduledTask[Print[RandomReal[]];If[x≥1,RemoveScheduledTask[$ScheduledTask]],1];

Cause the task to be stopped and removed:

x=1

Synchronize scheduled tasks using an auxiliary task:

n=1;Dynamic[FlipView[{Graphics[{Red,Disk[]}],Graphics[{Green,Disk[]}]},n]]

x1=CreateScheduledTask[StartScheduledTask[x3];RemoveScheduledTask[$ScheduledTask],{0.3}];
x2=CreateScheduledTask[n=1,1];
x3=CreateScheduledTask[n=2,1];

ScheduledTasks[]

StartScheduledTask[{x1,x2}];

The auxiliary task x1 was removed:

ScheduledTasks[]

Stop all scheduled tasks:

StopScheduledTask[ScheduledTasks[]];

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[$EvaluationEnvironment, paclet:ref/$EvaluationEnvironment])  ▪  (RefLink[$EvaluationCloudObject, paclet:ref/$EvaluationCloudObject])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Date and Time Functions, paclet:tutorial/DateAndTimeFunctions])}, {▪(RefLinkPlain[Controlling Infinite Evaluation, paclet:tutorial/ControllingInfiniteEvaluation])}})}})