TaskDefinition wil not run when not (live) connected to the sever

May 22, 2015 at 7:12 PM
I'dp have the following issue.

In a (webapplication) I create a taskdefinition
        var cmd = @"restart.cmd";
        var taskName = string.Format("restart_{0}", DateTime.Now.ToString("yyyyMMMMdd_HHmm"));
TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = taskName;
            td.Triggers.Add(new TimeTrigger() {StartBoundary = DateTime.Now});
            td.Actions.Add(new ExecAction(cmd, null, null));
            td.Settings.Compatibility = TaskCompatibility.V2_1;
            td.Settings.ExecutionTimeLimit = TimeSpan.FromMinutes(15); // also tried without this line
            td.Principal.LogonType = TaskLogonType.InteractiveToken; // also tried without this line               
            task = ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, @"domain\adminusername","pwd");
            // Remove the task we just created
            if (task != null)
            {
                task.Run();
                // Remove the task we just created
                ts.RootFolder.DeleteTask(taskName);
            }
The website is running on windows 2012. When I close the connection to the server, do not log off, the task is not runned any more. No error message, nothing. But it simply does not run.

Can there be a reason for this behaviour?

When I stay connected to the server it will keep continue to run.

Thanks in advance

Wijnand Beke
The Netherlands
Coordinator
May 25, 2015 at 11:33 PM
I guess my first question is why even use tasks? You are effectively just launching a process immediately either using a trigger or forcing the Run of the task. Seems like you are adding complexity where none is needed. If your code doesn't fully represent what you're trying to accomplish, I would offer two suggestions: 1) The TimeTrigger will only run once and you are telling it to run Now, which by the time the task is registered is a time in the past. 2) You are immediately deleting the task so if it had not run already, it won't have a chance to run. You may also consider setting the td.Settings.DeleteExpiredTaskAfter to something like 5 seconds so the task will run once and then delete itself.