2

I want a task to run every five minutes from a specific start date to a specific end date.

Documentation states syntax should be:

{start, timespec, end}$\qquad$ run between dates start and end

SessionSubmit[
 ScheduledTask[
  MessageDialog["Test"], {DatePlus[Now, Quantity[1, "Minutes"]], 
   Quantity[15, "Seconds"], DatePlus[Now, Quantity[2, "Minutes"]]}]]

Error message:

SessionSubmit::sched: $Failed is not an association containing recognized scheduling time specification.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
Zviovich
  • 9,308
  • 1
  • 30
  • 52

2 Answers2

5

This seems to be one of those times when the documentation is wrong. At least I could not get the described syntax to work any more than you could. However, I was able to contrive a work-around.

SessionSubmit[
  ScheduledTask[
    {DatePlus[Now, Quantity[1, "Minutes"]], 
     MessageDialog[Column[{"Test", DateString["Time"]}]]},
    {Quantity[15, "Seconds"], 4},
    AutoRemove -> True]];

This puts up the message dialog four times at 15 sec. intervals after a delay of one minute, thus given a series of dialog events with the timing you specified. Hope this helps.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
0

First, while the syntax you describe is not supported in M11.3, it will be supported in M12.

Second, I don't think the accepted answer works properly.

Here is my suggestion for a workaround:

System`Dump`iSessionSubmit[
    ScheduledTask[expr_, {start_?DateObjectQ, time_}, o___], 
    opts:OptionsPattern[]
] := With[{delay = DateDifference[Now, start]},
    SessionSubmit @ ScheduledTask[
        SessionSubmit[ScheduledTask[expr, time, o], opts],
        {delay, 1}
    ]
]

System`Dump`iSessionSubmit[
    ScheduledTask[expr_, {start_?DateObjectQ, time_, end_?DateObjectQ}, o___], 
    opts:OptionsPattern[]
] := With[{spec = quant[time]},
    If[spec === $Failed,
   $Failed,
        With[{delay = DateDifference[Now, start], count = Floor[DateDifference[start, end]/spec]},
            SessionSubmit @ ScheduledTask[
                SessionSubmit[ScheduledTask[expr, {time, count}, o], opts],
                {delay, 1}
            ]
        ]
    ]
]

quant["Daily"] = Quantity[1, "Days"];
quant["Hourly"] = Quantity[1, "Hours"];
quant["Weekly"] = Quantity[1, "Weeks"];
quant["Monthly"] = Quantity[1, "Months"];
quant["Yearly"] = Quantity[1, "Years"];
quant[n_?NumericQ] := Quantity[n, "Seconds"];
quant[q_Quantity] := q
quant[_] := $Failed

Example:

SessionSubmit @ ScheduledTask[
    Paste[InputNotebook[], ExpressionCell[DateString["Time"], "Output"]],
    {Now + Quantity[10, "Seconds"], 2, Now + Quantity[20, "Seconds"]}
];
"start: " <> DateString["Time"]

"start: 12:21:00"

"12:21:12"

"12:21:14"

"12:21:16"

"12:21:18"

"12:21:20"

Carl Woll
  • 130,679
  • 6
  • 243
  • 355