简体   繁体   English

使用任务计划程序创建重复任务

[英]Create repetitive task using Task Scheduler

I am using Task Scheduler library from here: taskscheduler.codeplex.com 我正在从此处使用Task Scheduler库:taskscheduler.codeplex.com

As per their examples, I am trying to create task with following behaviour: Task should run at every 1 hour for all 12 months including all days of the month. 按照他们的示例,我尝试创建具有以下行为的任务:任务应该在所有12个月(包括当月的所有天数)中每1小时运行一次。

Following code is doing this except task does not get repeated every 1 hour. 下面的代码执行此操作,除非任务不会每1小时重复一次。 It run once and then it runs on next day. 它运行一次,然后在第二天运行。

                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);

I have also tried TimeTrigger but that too does not repeat task. 我也尝试过TimeTrigger,但是也不会重复执行任务。 If I see the created task in Scheduled Task window, I see following: 如果在“计划任务”窗口中看到创建的任务,则会看到以下内容:

任务计划程序动作

If you see the red highlighted part, the task repetition is off. 如果看到红色突出显示的部分,则说明任务重复处于关闭状态。 I need to enable it so my task can execute at every hour a day. 我需要启用它,这样我的任务才能每天执行一次。 Any help would be great in this direction. 在这个方向上的任何帮助都会很棒。

Thanks, Jay 谢谢,周杰伦

I believe following line is culprit. 我相信以下行为是元凶。

trigger.Repetition.Duration = TimeSpan.FromHours(24);

You want to remove this line. 您要删除此行。 I wrote following program and it works as expected. 我写了下面的程序,它按预期工作。

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();
    }

This is how above task looks in Task Scheduler UI: 这是上述任务在Task Scheduler UI中的外观:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM