Task does not start if scheduled start is missed.

Oct 4, 2012 at 12:56 PM
Edited Oct 4, 2012 at 3:03 PM


I create a task that should start on a given date. Everything works fine as long as i don't miss a task (for example: computer was turned off the day, that the task should have started). Maybe i am missing something in code or any other suggestions would be very appreciated. Issue currently is on win7, haven't yet tested on xp or vista machine.

According to microsoft the StartWhenAvailable parameter only applies to time-based tasks where TimeTrigger (in my understanding) is a time-based task. So don't have any ideas what could be wrong.


Here is the method for creating a task.


public static void AddTask(string name, string description, DateTime date)
            using (TaskService service = new TaskService())
                TaskDefinition taskDef = service.NewTask();
                taskDef.RegistrationInfo.Description = description;

                taskDef.Settings.Enabled = true;
                taskDef.Settings.ExecutionTimeLimit = TimeSpan.Zero;
                if (IsWinVistaOrHigher())
                    taskDef.Settings.MultipleInstances = TaskInstancesPolicy.StopExisting;
                    taskDef.Settings.StartWhenAvailable = true;
                    taskDef.Settings.RestartCount = 10;
                    taskDef.Settings.RestartInterval = new TimeSpan(1, 0, 0, 0);
                    taskDef.Settings.Compatibility = TaskCompatibility.V2;

                    taskDef.Triggers.Add(new TimeTrigger(date) { StartBoundary = date.AddHours(-1), Enabled = true });
                    taskDef.Settings.Compatibility = TaskCompatibility.V1;

                    taskDef.Triggers.Add(new DailyTrigger { StartBoundary = date.AddHours(-1), Enabled = true });

                taskDef.Actions.Add(new ExecAction(Environment.GetCommandLineArgs()[0]));

                service.RootFolder.RegisterTaskDefinition(name, taskDef);
Oct 4, 2012 at 7:55 PM

Your TimeTrigger instance needs to either set the EndBoundary property or change the RestartInterval to TimeSpan.Zero (for infinite) as per the conditions on the link you provided. You may also want to consider looking at the DailyTrigger and not set the Restart.. properties but set an EndBoundary property for 10 days from today.