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

Create daily 9h Task Schedule in Windows 7

Topics: Errors, Setup
Feb 7, 2014 at 3:52 PM
Edited Feb 7, 2014 at 3:52 PM
Hi guys,

I am trying to create a schedule task at the installation time on a Windows 7 32bits, but there is something wrong because it returns me an exception "LogonType". Another thing, I want to create a schedule to run every day at 9h morning.

Can you help me out on this one? What is missing?!

This is the code that I am using:
using (TaskService ts = new TaskService())
                {
                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = "Daily trigger";
                    td.Principal.LogonType = TaskLogonType.InteractiveToken;
                    
                    DailyTrigger dt = new DailyTrigger();
                    dt.Repetition.Duration = TimeSpan.FromHours(9); // is this ok to run every day at 9h?????
                    td.Triggers.Add(dt);


                    // Create an action that will launch Notepad whenever the trigger fires
                    td.Actions.Add(new ExecAction("c:\\Launcher.exe"));

                    // Register the task in the root folder
                    ts.RootFolder.RegisterTaskDefinition(@"Scheduler", td);
                }
Coordinator
Feb 10, 2014 at 3:25 PM
// 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 = "Daily trigger";
   td.Principal.LogonType = TaskLogonType.InteractiveToken;

   // Add a trigger that will fire the task at this time every other day
   td.Triggers.Add(new DailyTrigger { StartBoundary = DateTime.Today.AddHours(9) });

   // Add an action that will launch Notepad whenever the trigger fires
   td.Actions.Add(new ExecAction("C:\\Launcher.exe"));

   // Register the task in the root folder
   ts.RootFolder.RegisterTaskDefinition("Scheduler", td);
}
Feb 10, 2014 at 4:00 PM
First of all thanks for your quick replay Dahall,

However the problem persists, everytime I try to depoy the setup on a Win7 machine it gives me an exception "Exception: (41,4):LogonType: " nothing more :(
On a XP machine all runs smooth.

From what I understood I am missing something related with the "RegisterTaskDefinition" besause once I use this line:
ts.RootFolder.RegisterTaskDefinition("Scheduler", td, TaskCreation.Create, "SYSTEM", null, TaskLogonType.ServiceAccount);
the task is created but I dont understand the usage of the "SYSTEM" for the username, can I presume that with this username it will be runnable in all machines? Or do I have to supply the username/password on all Win7 machines in order to deploy the task?

Thanks in advance!