Multiple Triggers to create Multiple Schedules

Mar 2, 2010 at 6:26 AM

I want my scheduler to run when on "At SystemStartup" and also on  "At Logon". This is because, when the application starts at StartUp and user logs off than the application stops. So, I want to start it again on UserLogon.

Here is the code.

            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = "MyApplication";
            td.Triggers.Add(new Microsoft.Win32.TaskScheduler.BootTrigger());
            td.Triggers.Add(new Microsoft.Win32.TaskScheduler.LogonTrigger());
            td.Actions.Add(new ExecAction("My Application Path", null, "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup"));
            ts.RootFolder.RegisterTaskDefinition("MyApplication", td);

But it is not creating multiple schedules.

Coordinator
Mar 3, 2010 at 5:10 PM

The code is correct, so I'm guessing there is a permission problem. You need to make sure that the user credentials supplied when instantiating the TaskService have Administrator rights for the system. If that is the case, you may want to check code permissions if this is running under Windows 7 or Server 2008 R2.

Mar 4, 2010 at 4:23 AM

I dont think that there is any permission problem. Because the above code is creating multiple schedules but,

1. "Show Multiple Schedules" check box is not getting checked, infact it becomes disabled.

2. Both the schedules (one at the top drop down and other at the middle drop down) show the same schedule as "At System Startup".

Please try the same at your end using the above code snippet and you will get to know the problem.

Coordinator
Mar 4, 2010 at 6:14 AM

I think I need more information then. I have run the snippet under Vista (and the 2.0 library) and it works as expected. Can you provide the OS you are working under and ensure you are running the latest version of this managed library?

Mar 4, 2010 at 12:20 PM

I tried this on both XP and 2003 server. It didn't worked.

I am using the release version 1.4.2

 

Coordinator
Mar 4, 2010 at 8:25 PM

I just ran this code on an XP SP2 machine and it worked correctly. I'm not sure why yours if failing.

using (TaskService ts = new TaskService())
{
   TaskDefinition td = ts.NewTask();
   td.RegistrationInfo.Description = "MyApplication";
   td.Triggers.Add(new BootTrigger());
   td.Triggers.Add(new LogonTrigger());
   td.Actions.Add(new ExecAction("calc.exe", null, null));
   Task t = ts.RootFolder.RegisterTaskDefinition("MyApplication", td);
   t.ShowPropertyPage();
   ts.RootFolder.DeleteTask("MyApplication");
}

This was run while logged into an account in the Adminstrators group. When looking at the "Schedule" property page, it does show the disabled "Show Multiple Schedules" check box, and there are two items in the combobox on top: "1. Run at system startup", and "2. Run at user logon".