What is the preferred way to check whether a scheduled task is active or not?
I'll outline my solution below, but I am not sure this is indeed the best way to do it.
A scheduled task is created like this:
In[1]:= myTask=CreateScheduledTask[Print["task 1"], 30]
Out[1]= ScheduledTaskObject[1,Print[task 1],{30,Infinity},Automatic,False]
We can check for the existing tasks like this:
In[2]:= ScheduledTasks[]
Out[2]= {ScheduledTaskObject[1,Print[task 1],{30,Infinity},Automatic,False]}
The last entry in the ScheduledTaskObject
(True
or False
) appears to indicate whether the task is started or not.
Now let's start the task, and compare the contents of the variable myTask
with the list returned by ScheduledTasks[]
.
In[3]:= StartScheduledTask[myTask]
Out[3]= ScheduledTaskObject[1,Print[task 1],{30,Infinity},Automatic,False]
In[4]:= {ScheduledTasks[],myTask}
Out[4]= {{ScheduledTaskObject[1,Print[task 1],{30,Infinity},Automatic,True]},
ScheduledTaskObject[1,Print[task 1],{30,Infinity},Automatic,False]}
Note that they differ. The variable shows False
while ScheduledTasks[]
shows false. This demonstrates that the variable does not actually hold the task object. By modifying the variable myTask
directly we can't modify the task. The real state of the task is returned by ScheduledTasks[]
.
It looks reasonable to assume though that the first entry in the ScheduledTaskObject
expression is a unique number corresponding to the task. So any manual operation performed on the task could perhaps use this number as a "handle", and we could check the state of task with identifier 1
like this:
Cases[ScheduledTasks[], ScheduledTaskObject[1,__,state_] :> state]
I am not sure at all though that this approach (using the identifier from the ScheduledTaskObject
) is correct. I have noticed situations when there were already some tasks in a fresh kernel (probably due to the front end --- I had several notebook open, some with dynamic elements, and I've been experimenting with tasks), and sometimes ScheduledTasks[]
returns several tasks with the same identifier (usually 0
).
In summary:
Is it a reliable way to handle tasks by their identifier (the first number from their
ScheduledTaskObject
)?What is the correct way to query the state of a scheduled task, given the expression we get from
CreateScheduledTask
when we first create it?
EDIT: If it's possible, it would also be nice to have a Dynamic cell showing the state of a scheduled task, without using too much CPU. Does anyone have any ideas about how to do this?