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

Setting DailyTrigger.Repetition.Interval throws exception in V1

Aug 11, 2010 at 4:01 PM

When I try to set the DailyTrigger.Repetition.Interval property on Windows Server 2003, I get an exception.  The same code running on Windows 7 Ultimate works fine.  If I comment out the line of code that sets the Interval, the task is created however it doesn't have any interval.

 

My Code is:

using (TaskService ts = new TaskService())
            {
                Task igisMonitor = ts.GetTask(TASK_NAME);
                if (null == igisMonitor)
                {
                    try
                    {
                        TaskDefinition igisMonTaskDef = ts.NewTask();
                        igisMonTaskDef.RegistrationInfo.Author = TASK_AUTHOR;
                        igisMonTaskDef.RegistrationInfo.Description = TASK_DESCRIPTION;
                        igisMonTaskDef.Settings.Enabled = true;
                        igisMonTaskDef.Settings.ExecutionTimeLimit = TimeSpan.FromMinutes(10);
                        igisMonTaskDef.Settings.RunOnlyIfIdle = false;
                        
                        //Set the compatability level for V2 tasks and
                        //start when available
                        if (isNewVersion)
                        {
                            igisMonTaskDef.Settings.StartWhenAvailable = true;
                            igisMonTaskDef.Settings.Compatibility = TaskCompatibility.V2;
                        }

                        //Create a trigger for this task
                        try
                        {
                            DailyTrigger runTrigger = (DailyTrigger)igisMonTaskDef.Triggers.Add(new DailyTrigger { DaysInterval = 1 });
                            runTrigger.Repetition.Duration = TimeSpan.FromMinutes(TASK_RUN_INTERVAL);
                            runTrigger.Repetition.StopAtDurationEnd = true;
                            runTrigger.StartBoundary = DateTime.Now.Date;

                            if (isNewVersion)
                            {
                                runTrigger.Repetition.Interval = TimeSpan.FromMinutes(TASK_RUN_INTERVAL);
                            }
                            else
                            {
                                runTrigger.Repetition.Interval = TimeSpan.FromMinutes(TASK_RUN_INTERVAL);
                            }
                        }
                        catch (Exception runTriggerEx)
                        {
                            string message = "Error adding DailyTrigger::" + runTriggerEx.ToString();
                            throw new Exception(message);
                        }

                        try
                        {
                            igisMonTaskDef.Actions.Add(new ExecAction(POWERSHELL_EXE_PATH, _scriptCommand, _workingDir));
                        }
                        catch (Exception addExecActionEx)
                        {
                            string message = "Error adding ExecAction::" + addExecActionEx.ToString();
                            throw new Exception(message);
                        }


                        //Register the task
                        ts.RootFolder.RegisterTaskDefinition(TASK_NAME, igisMonTaskDef);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }

In the above code, isNewVersion is a bool that is set by the constructor of my class that contains the method which this code comes from.

 

The exception I get is:

System.ArgumentException: Value does not fall within the expected range.   at Microsoft.Win32.TaskScheduler.V1Interop.ITaskTrigger.SetTrigger(TaskTrigger& Trigger)   at Microsoft.Win32.TaskScheduler.RepetitionPattern.Bind()   at Microsoft.Win32.TaskScheduler.RepetitionPattern.set_Interval(TimeSpan value)   at IntergisMonitor.TaskSchedulerInterface.AddScheduledTask() in C:\Source\Implementation\IntergisMonitor\IntergisMonitor\TaskSchedulerInterface.cs:line 144   at IntergisMonitor.TaskSchedulerInterface.AddScheduledTask() in C:\Source\Implementation\IntergisMonitor\IntergisMonitor\TaskSchedulerInterface.cs:line 169   at IntergisMonitor.FormMain.buttonCreateScheduledTask_Click(Object sender, EventArgs e) in C:\Source\Implementation\IntergisMonitor\IntergisMonitor\FormMain.cs:line 46   at System.Windows.Forms.Control.OnClick(EventArgs e)   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)   at System.Windows.Forms.Control.WndProc(Message& m)   at System.Windows.Forms.ButtonBase.WndProc(Message& m)   at System.Windows.Forms.Button.WndProc(Message& m)   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I have the latest version of the dll from the download page

Coordinator
Aug 17, 2010 at 7:53 PM

This sounds like a limitation with the core library. The only constraint on the ITaskTrigger.SetTrigger method from the Microsoft documentation is that interval must be less than duration. This is your problem as you have set them both to the same value. Make sure runTrigger.Repetition.Interval is less than the runTrigger.Repetition.Duration value.

Aug 17, 2010 at 8:08 PM

Good call, that did it!  Thanks, your library rocks!