Multiple schedules are created unexpectedly

Dec 15, 2010 at 7:52 PM


When I run the following code (WinXP SP3, .NET 3.5), I always get two schedules: one at DateTime.Now to run daily with no end date, and another with the expected start and end dates to run once instead of daily. I am trying to get a task that runs daily starting tomorrow for 7 days... just one schedule.

             TaskService ts = new TaskService();

            Version ver = ts.HighestSupportedVersion;
            bool newVer = (ver >= new Version(1, 2));

            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = "Task Description";
            if (newVer) td.Principal.RunLevel = TaskRunLevel.Highest;
            td.Principal.UserId = "System";
            td.Principal.LogonType = TaskLogonType.ServiceAccount;
            DateTime start = DateTime.Today.AddDays(1.0d);
            TimeTrigger tTrigger = (TimeTrigger)td.Triggers.Add(new TimeTrigger(start));
            tTrigger.EndBoundary = DateTime.Today.AddDays(7.0d);

            td.Triggers.Add(new DailyTrigger(1));

            td.Actions.Add(new ExecAction(

            ts.RootFolder.RegisterTaskDefinition("TaskName", td);

I am not sure how I am creating multiple schedules. Can you tell me what I am doing wrong?

Thanks so much!

Dec 15, 2010 at 11:00 PM

You are adding (Triggers.Add) two different triggers: A TimeTrigger which will fire once on the next day at midnight and a DailyTrigger which will run every day indefinitely at the time you run the code. I believe the following code will accomplish what you need. Replace all four lines associated with creating and adding triggers with the following line.

td.Triggers.Add(new DailyTrigger { StartBoundary = DateTime.Now.AddDays(1), EndBoundary = DateTime.Now.AddDays(8) });