Trouble With RunScheduledTask

I seem to be having trouble with RunScheduledTask[]. Here is my code…

tasks = {"Task A", "Task B", "Task C", "Task D"};
tooEarly = TimeObject[{8, 0, 0}];
tooLate = TimeObject[{21, 0, 0}];
taskPicker[tooEarly_, tooLate_] := Module[{task, now},
  now = TimeObject[];
  If[tooEarly < now && now < tooLate,
   task = RandomChoice[tasks];
   MessageDialog[StringJoin["Time to do task: ", task, " "]];
   
   ]
  ]
RunScheduledTask[taskPicker[tooEarly, tooLate], 5];

If I run that, my MessageBox pops up as expected, 5 seconds after I hit Shift-Return.

But the, 5 seconds later, I get the following in the Messages window…

…and I never see my dialog again.

It I take out the lines in my taskPicker function that involve querying for the current time, it all works…

taskPicker[tooEarly_, tooLate_] := Module[{task, now},
  
  task = RandomChoice[tasks];
  MessageDialog[StringJoin["Time to do task: ", task, " "]];
  
  ]

It is as though you cannot run TimeObject[] within a scheduled task.

You have to use:

now = TimeObject[Now];

why? I don’t know, Ask WRI. :slight_smile: