({{, RunScheduledTask[expr] 
schedules and starts a local scheduled 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.}, {, RunScheduledTask[(RefLink[CloudObject, paclet:ref/CloudObject])[…]]
asynchronously executes an existing cloud task or document generator.}})

OpenersNotesSection(Spacer1)Details and Options

For local tasks, RunScheduledTask returns a (RefLink[ScheduledTaskObject, paclet:ref/ScheduledTaskObject]) expression representing the started task. For cloud tasks and document generators, RunScheduledTask returns the task's (RefLink[CloudObject, paclet:ref/CloudObject]).

RunScheduledTask creates a task to be run locally in the current Wolfram Language session. No task state is preserved between sessions.

RunScheduledTask[x] is effectively equivalent to (RefLink[StartScheduledTask, paclet:ref/StartScheduledTask])[(RefLink[CreateScheduledTask, paclet:ref/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.

By default, if the time necessary to evaluate expr is larger than time, the next pending evaluation is skipped.

OpenersPrimaryExamplesSection(Spacer1)Examples  (15)

OpenersExampleSection0(Spacer1)Basic Examples  (11)

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

n=1;Dynamic[FlipView[{Graphics[{Red,Disk[]}],Graphics[{Green,Circle[{0,0},1], Text["OK",{0,0}]}]},n]]

RunScheduledTask[n=2,{3}];

RemoveScheduledTask[ScheduledTasks[]];

Perform an action the specified number of times:

n=0;Dynamic[n]
RunScheduledTask[n++,{1,3}];

RemoveScheduledTask[ScheduledTasks[]];

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

RunScheduledTask[MessageDialog["text"],{1},AbsoluteTime[]+5]

Specify a date to activate the task:

RunScheduledTask[MessageDialog["text"],{1},AbsoluteTime[{2010,3,12,15,16,0}]]

Front end versus kernel clocks:

RunScheduledTask[x=DateString[]];

Dynamic[x]
Dynamic[Refresh[DateString[],UpdateInterval->1]]

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

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

x=0;RunScheduledTask[If[0≤x<1,Print[RandomReal[]]],1];

Pause the evaluation without stopping the task:

x=1

Re-enable the evaluation:

x=0

RemoveScheduledTask[ScheduledTasks[]];

OpenersExampleSection1(Spacer1)Scope  (2)

RunScheduledTask causes cloud scheduled tasks to evaluate asynchronously, as for calendar-triggered evaluations:

obj=CloudDeploy[ScheduledTask[
SendMail[<|"To"->$WolframID,"Subject"->"documentation example","Body":>DateObject[]|>],
{Tomorrow}
]]

Trigger the task to run asynchronously:

With[{o=obj},RunScheduledTask[o]]

Delete the task:

RemoveScheduledTask[obj]

RunScheduledTask causes document generators to evaluate asynchronously. Logging, distribution, and archiving occur as for calendar-triggered evaluations:

obj=CloudDeploy[DocumentGenerator[File["ExampleData/BasicTemplate.nb"],None,DeliveryFunction->Automatic]]

Trigger the generator to run asynchronously:

With[{o=obj},RunScheduledTask[o]]

Delete the generator:

RemoveScheduledTask[obj]

OpenersExampleSection2(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)GeneratedDocumentBinding  (1)

Use the (RefLink[GeneratedDocumentBinding, paclet:ref/GeneratedDocumentBinding]) option to override a document generator's configured driver. Deploy a document generator:

co=CloudDeploy[
DocumentGenerator[File["ExampleData/BasicTemplate.nb"],<|"author":>$WolframID|>,None
]
]

Trigger the generator to run asynchronously, overriding the driver:

With[{o=co},RunScheduledTask[o,GeneratedDocumentBinding-><|"author"->"Anonymous"|>]]

Delete the generator:

RemoveScheduledTask[co]

OpenersExampleSection3(Spacer1)Possible Issues  (1)

RunScheduledTask has attribute (RefLink[HoldFirst, paclet:ref/HoldFirst]) and will interpret cloud object arguments as expressions to be evaluated in a local scheduled task:

obj=CloudDeploy[ScheduledTask[f,None]]

This will evaluate obj once per second in a local scheduled task:

localObj=RunScheduledTask[obj]

Delete the local task:

RemoveScheduledTask[localObj]

A (RefLink[With, paclet:ref/With]) or (RefLink[Apply, paclet:ref/Apply]) construct ensures cloud execution:

With[{o=obj},RunScheduledTask[o]]

RunScheduledTask@@{obj}

Delete the cloud task:

RemoveScheduledTask[obj]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[AbsoluteTime, paclet:ref/AbsoluteTime])  ▪  (RefLink[Clock, paclet:ref/Clock])  ▪  (RefLink[Refresh, paclet:ref/Refresh])  ▪  (RefLink[ScheduledTask, paclet:ref/ScheduledTask])  ▪  (RefLink[DocumentGenerator, paclet:ref/DocumentGenerator])  ▪  (RefLink[CloudDeploy, paclet:ref/CloudDeploy])}})}})

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