Windows TaskScheduler DailyTrigger run for a duration of

时间:2015-06-26 10:23:38

标签: c# windows-scheduler

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 个答案:

答案 0 :(得分:0)

编辑:我注意下面是一个不同的类,OP可能已下载a library from Codeplex。以下内容仍适用,只是Repetition.IntervalRepetition.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

     

获取或设置要重复运行的任务的执行之间的分钟数。

     

[...]

     

任务继续重复运行,直到指定的间隔为止   DurationMinutes属性到期。 IntervalMinutes值是   从上一次执行开始算起。

     

IntervalMinutes值必须小于DurationMinutes值。

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

  

<强> DurationMinutes

     

获取或设置触发器触发后触发器保持活动状态的分钟数。

     

[...]

     

此属性与IntervalMinutes属性一起使用   在一段时间内反复运行任务。例如,开始一个   任务于上午8点并反复重启,直到下午5点   DurationMinutes值为540分钟(9小时)。

     

DurationMinutes值还可用于终止正在运行的任务   在任务的DurationMinutes属性到期后。

     

使用KillAtDurationEnd属性指定任务   在DurationMinutes到期后终止。 DurationMinutes的值必须大于或等于IntervalMinutes   设置。