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

Create Task at LogOn

Aug 8, 2014 at 3:23 AM
I need to create a Task at LogOn
which repeats after every 1 hr
and should Run whether UserLogged On or Not
also should run with Highest Privileges

and it should run for the first time as soon as it gets created for the first time
can anyone help me creating a code snippet for it.

Aug 8, 2014 at 7:16 AM
OKie I was able to create a Task and execute at time of Task Creation

Two Things which I'm missing is
1.Setting task as "Run Task Whether User is logged In or Not
  1. I checked after the interval the "Next Run Time " is coming blank the execution is not taking place after a fixed interval .
    Here is the code snippet
private void button1_Click(object sender, EventArgs e)

            using (TaskService ts=new TaskService() )
                TaskDefinition td = NewMethod(ts);

                LogonTrigger lt = new LogonTrigger();

                lt.Repetition.Interval = TimeSpan.FromMinutes(2);
                lt.Repetition.Duration = TimeSpan.Zero;
                lt.StartBoundary = DateTime.Now;
                lt.EndBoundary = DateTime.MaxValue;


                td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

                TaskFolder tf = ts.RootFolder;
                // Register the task in the root folder
                  //ts.RootFolder.RegisterTaskDefinition("Test", td);
                Task task = tf.RegisterTaskDefinition("test", td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);

private static TaskDefinition NewMethod(TaskService ts)
            string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            TaskDefinition td = ts.NewTask();

            td.Principal.UserId = user;
            td.Principal.LogonType = TaskLogonType.InteractiveToken;

            td.RegistrationInfo.Description = "xyz";
            td.RegistrationInfo.Author = "xyz Sync";
            td.RegistrationInfo.Date = DateTime.Now;

            td.Principal.RunLevel = TaskRunLevel.Highest;
            td.Settings.Priority = ProcessPriorityClass.High;            
            td.Settings.StopIfGoingOnBatteries = false;
            td.Settings.DisallowStartIfOnBatteries = false;
            td.Settings.StartWhenAvailable = true;
            td.Settings.RunOnlyIfNetworkAvailable = true;
            td.Settings.Enabled = true;
            td.Settings.Hidden = false;
            //td.Settings.RunOnlyIfLoggedOn = false;

            return td;
Aug 8, 2014 at 4:50 PM
Your logic doesn't work. You can't have a LogonTrigger that is able to run when the user is not logged on. What is the requirement? Is it that you need a task to run using a specific user's credentials every hour?