Windows TaskScheduler DailyTrigger run for a duration of

2.3k views Asked by At

I'm creating a trigger for a task in Windows using Microsoft.Win32.TaskScheduler.DailyTrigger to run daily at 8am. That task repeats every hour but I want it to stop after 10 hours until it fires up again the next day.

In the Windows task scheduler application, under trigger you have something like "Repeat task every 1 hour for a duration of 10 hours".

The repeat task every hour I can do, but I can't find a way to do the "for a duration of". This is the code I have to set up the trigger so far, startTime is a DateTime set to 8am today.

var dailyTrigger = new DailyTrigger();
dailyTrigger.Repetition.Interval = TimeSpan.FromHours(1);
dailyTrigger.StartBoundary = startTime;
dailyTrigger.ExecutionTimeLimit = TimeSpan.FromMinutes(59);

I could do it with multiple triggers, but I was thinking if the application interface allows it there probably is a way to do it in code.

1

There are 1 answers

1
Peter On BEST ANSWER

EDIT: I noticed below is a different class, and the OP probably downloaded a library from Codeplex. The below still applies, it's just Repetition.Interval and Repetition.Duration.

// Set the time in between each repetition of the task after it starts to 30 minutes.
tt.Repetition.Interval = TimeSpan.FromMinutes(60); // Default is TimeSpan.Zero (or never)
// Set the time the task will repeat to 1 day.
tt.Repetition.Duration = TimeSpan.FromDays(1); // Default is TimeSpan.Zero (or never)

https://msdn.microsoft.com/en-us/library/office/microsoft.office.excel.server.addins.computecluster.taskscheduler.trigger.intervalminutes(v=office.12).aspx

IntervalMinutes

Gets or sets the number of minutes between executions for a task that is to run repeatedly.

[...]

The task continues to run repeatedly until the interval specified in the DurationMinutes property expires. The IntervalMinutes value is counted from the start of the previous execution. The

IntervalMinutes value must be less than the DurationMinutes value.

https://msdn.microsoft.com/en-us/library/office/microsoft.office.excel.server.addins.computecluster.taskscheduler.trigger.durationminutes(v=office.12).aspx

DurationMinutes

Gets or sets the number of minutes that the trigger remains active after the trigger fires.

[...]

This property is used in conjunction with the IntervalMinutes property to run a task repeatedly for a period of time. For example, to start a task at 8:00 A.M. and repeatedly restart it until 5:00 P.M., the DurationMinutes value would be 540 minutes (9 hours).

The DurationMinutes value can also be used to terminate a running task after the DurationMinutes property for the task expires.

You use the KillAtDurationEnd property to specify that the task is terminated after its DurationMinutes expires. The value of DurationMinutes must be greater than or equal to the IntervalMinutes setting.