This project has moved and is read-only. For the latest updates, please go here.

Only a single week can be set with Task Scheduler 1.0 for MonhlyDOW trigger

Jan 23, 2012 at 4:41 PM

Hi,

I want to create MonthlyDOWTrigger using C#. I am using Microsoft.Win32.Scheduler.dll.

On providing WhichWeek.AllWeeks , its throwing exception "Only a single week can be set with Task Scheduler 1.0."

We can assign multiple weeks if created MonthlyDOW Trigger through Task Scheduler, but could not be done using Microsoft.Win32.Scheduler.dll.

Can anyone help, how to achieve this? Following is the code:

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

                // Create a trigger that will fire the task at this time every other day
                td.Triggers.Add(new MonthlyDOWTrigger(DaysOfTheWeek.Monday|DaysOfTheWeek.Saturday, MonthsOfTheYear.AllMonths,   WhichWeek.AllWeeks));

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

                // Remove the task we just created
                ts.RootFolder.DeleteTask("Test");
            }

Thanks

Jan 23, 2012 at 9:01 PM

The exception is correct. You are only able to use the Task Scheduler to create a MonthlyDOW trigger for mulitple weeks if the task is able to be used on Windows versions Vista and later. In the Task Scheduler, if you set the task to "Configure for:" = "Windows Server 2003, Windows XP, or Windows 2000" on the General tab, you will not be able to select multiple weeks when creating the trigger and create the task. This is a limitation of V1 of the Task Scheduler and I know of no work around other than creating multiple triggers.

Jan 27, 2012 at 2:16 PM

Hi,

I'm having the same problem as anandps1. I directly create an instance of TaskEditDialog using

TaskEditDialog ted = new TaskEditDialog();

Upon selecting more than one week for a MonthlyDOWTrigger, the exception mentioned by anandps1 is thrown.

 

Any help would be appreciated. Thanks,

Ara43

Jan 27, 2012 at 3:40 PM

Hi,

Since there is limitation in selecting multiple weeks, I created multiple triggers for each week selected as indicated by dahall in the reply.

 

 

Jan 27, 2012 at 7:09 PM

Yes, but the exception occurs with TriggerEditDialog.SupportV1Only = false, i.e., when the Windows Vista and 7 Task Scheduler *should* be supported. This seems wrong to me.

Jan 27, 2012 at 11:33 PM

That flag set to false enables the new triggers available in V2 and allows multiple weeks to be selected. When used in conjunction with the TaskEditDialog, that flag gets set based on the value of the Settings.Compatibility property of the TaskDefinition passed in to initialize the dialog.

Jan 30, 2012 at 2:49 PM

Hi dahall,

I have experimented a bit more. The following sample code fails for me:

TaskService ts = new TaskService();
TaskDefinition td = ts.NewTask();
Trigger timeTrigger = td.Triggers.AddNew(TaskTriggerType.Time);
TriggerEditDialog triggerEditDialog = new TriggerEditDialog(timeTrigger, false);
triggerEditDialog.ShowDialog();

The initial timeTrigger is V2, because (I'm using Win 7.) TriggerCollection.AddNew() calls Trigger(V2Interop.ITrigger iTrigger). When I use the dialog to change the TimeTrigger to a MonthlyDOWTrigger, I get an exception. It seems to be thrown because in TriggerEditDialog.monthlyDaysRadio_CheckedChanged(), new MonthlyDOWTrigger() is called, replacing the V2 trigger by a V1 trigger.

Thanks

 

Jan 30, 2012 at 8:20 PM

Thank you Ara43 for helping me find a bug. It has been corrected in source code release 74464.

Jan 31, 2012 at 8:56 AM

Thank you for solving this issue!