Jay*_*aty 1 c# scheduled-tasks
我正在从此处使用Task Scheduler库:taskscheduler.codeplex.com
按照他们的示例,我试图创建具有以下行为的任务:任务应该在所有12个月(包括当月的所有天)中每1小时运行一次。
下面的代码执行此操作,除非任务不会每1小时重复一次。它运行一次,然后在第二天运行。
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "sample task";
// Create a trigger that will execute very 1 hour.
var trigger = new MonthlyTrigger();
trigger.StartBoundary = DateTime.Now + TimeSpan.FromSeconds(60);
trigger.Repetition.Interval = TimeSpan.FromHours(1);
trigger.Repetition.Duration = TimeSpan.FromHours(24);
List<int> days = new List<int>();
for (int i = 1; i < 32; i++)
{
days.Add(i);
}
trigger.DaysOfMonth = days.ToArray();
td.Triggers.Add(trigger);
td.Actions.Add(new ExecAction(Assembly.GetEntryAssembly().Location));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(@"RemoteClient Task", td);
Run Code Online (Sandbox Code Playgroud)
我也尝试过TimeTrigger,但是也不会重复执行任务。如果在“计划任务”窗口中看到创建的任务,则会看到以下内容:

如果看到红色突出显示的部分,则说明任务重复处于关闭状态。我需要启用它,这样我的任务才能每天执行一次。在这个方向上的任何帮助都会很棒。
谢谢,周杰伦
我相信以下行为是元凶。
trigger.Repetition.Duration = TimeSpan.FromHours(24);
Run Code Online (Sandbox Code Playgroud)
您要删除此行。我写了下面的程序,它按预期工作。
static void Main(string[] args)
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Does something";
// Add a trigger that, starting now, will fire every day
// and repeat every 1 minute.
var dt = new DailyTrigger();
dt.StartBoundary = DateTime.Now;
dt.Repetition.Interval = TimeSpan.FromSeconds(60);
td.Triggers.Add(dt);
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition("Test", td);
}
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
这是上述任务在Task Scheduler UI中的外观:

| 归档时间: |
|
| 查看次数: |
5144 次 |
| 最近记录: |