This project has moved. For the latest updates, please go here.

Problems creating daily trigger

Jan 25, 2012 at 10:54 AM
Edited Jan 25, 2012 at 10:56 AM

I have some problems creating a daily trigger with repetition.

my code is copied from the example page and looks like following:

// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.Principal.LogonType = TaskLogonType.InteractiveToken;

var trigger = new DailyTrigger();
trigger.Repetition.Interval = TimeSpan.FromHours(2);
trigger.StartBoundary = DateTime.Now.AddMinutes(2);

// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(trigger);

// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", fileName));

// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(taskName, td);

Under win 7 works like a charm, but under win xp throws an exception 

Value does not fall within the expected range.
at Microsoft.Win32.TaskScheduler.V1Interop.ITaskTrigger.SetTrigger(TaskTrigger& Trigger)   
at Microsoft.Win32.TaskScheduler.Trigger.SetV1TriggerData() in C:\Code\TaskService\Trigger.cs:line 606   
at Microsoft.Win32.TaskScheduler.Trigger.Bind(ITask iTask) in C:\Code\TaskService\Trigger.cs:line 556   
at Microsoft.Win32.TaskScheduler.TriggerCollection.Add(Trigger unboundTrigger) in C:\Code\TaskService\TriggerCollection.cs:line 174

Since i have found the following topic Problem between Version 1 and Version 2, i changed the code to the following:

 

// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.Principal.LogonType = TaskLogonType.InteractiveToken;

var trigger = new DailyTrigger();
trigger.Repetition.Interval = TimeSpan.FromHours(2);
// Changed for xp compatibility
trigger.Repetition.Duration = TimeSpan.FromHours(3);
trigger.StartBoundary = DateTime.Now.AddMinutes(2);

// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(trigger);

// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", fileName));

// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(taskName, td);

Now it works as expected.

as @daok wrote in Problem between Version 1 and Version 2 I think the API should handle those type of stuff for V1 and maybe trow a meaningful exception.

Best regards Max

Coordinator
Jan 30, 2012 at 6:23 PM

Thanks for posting your resolution.