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

DeleteExpiredTaskAfter?

Topics: Errors, Examples
Jun 4, 2013 at 9:39 AM
Hey ho all ...
first of all, i'm new in c# and my english is very poor :(

So now my question ..
I'm creating a Task with the following Code
td.RegistrationInfo.Description = " Some Text which describes my task";
td.Principal.LogonType = TaskLogonType.InteractiveToken;

TimeTrigger tt = new TimeTrigger();
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddHours(2);
TimeSpan interval = d2 - d1;

tt.StartBoundary = d1;
tt.EndBoundary = d2.AddDays(1);
td.Triggers.Add(tt);
td.Settings.DisAllowStartIfOnBatteries = false;
td.Settings.DeleteExpiredTasksAfter = interval;


td.Actions.Add(new ExecAction("Notepad",null));

ts.RootFolder.RegisterTaskDefinition(@"Definition Example", td, TaskCreation.Create, HttpContext.Current.User.Identity.Name, null);
Now my Task will be created but there is a issue with the Expiration Time / NextRunTIme
The Task will be created like the following example:
Name = "Example"
Status = "Ready"
Triggers = At 10:36 on 04.06.2013 - Trigger expires at 05.06.2013 12:36:00
Next RunTime = "05.06.2013 14:36:00"

How can i set the Next Runtime correct?
In my Scenario i think the next Runtime should be set to 04.06.2013 10.36.00 and not to the next day 2 hours after the expiration ...
Any Hint? Tips? Tricks?

Kind regards,
Kai
Jun 15, 2013 at 10:29 PM
Edited Jun 15, 2013 at 10:30 PM
With a TimeTrigger, the EndBoundary really doesn't mean anything as the StartBoundary defines when the task will execute. For a TimeTrigger, NextRunTime will return DateTime.MinDate if the StartBoundary has already passed. To delete the task immediately after all triggers have executed, set the value to TimeSpan.FromSeconds(1). StartBoundary is set to DateTime.Now by default.

I would rewrite your code to the following:
td.RegistrationInfo.Description = "Some Text which describes my task";
td.Principal.LogonType = TaskLogonType.InteractiveToken;

td.Triggers.Add(new TimeTrigger());
td.Settings.DisAllowStartIfOnBatteries = false;
td.Settings.DeleteExpiredTasksAfter = TimeSpan.FromSeconds(1);

td.Actions.Add(new ExecAction("Notepad", null));

ts.RootFolder.RegisterTaskDefinition(@"Definition Example", td, TaskCreation.Create, HttpContext.Current.User.Identity.Name, null);
Jun 21, 2013 at 2:56 PM
Thank you dahall for your code.
But this is not usefull for my scenario :(

I have to set a starttime
So just a example:
Programm start creating a task
The task will be generated to run in 30 minutes
after 1 day the task have to be removed automaticaly 
If i use your code example it realy works, but just for tasks that will be run direkt after creation
Jun 21, 2013 at 5:26 PM
The StartTime property defines when a TimeTrigger will run. For your example of starting the task in 30 minutes use:
td.Triggers.Add(new TimeTrigger(DateTime.Now + TimeSpan.FromMinutes(30)));
Jun 27, 2013 at 1:38 PM
dahall wrote:
The StartTime property defines when a TimeTrigger will run. For your example of starting the task in 30 minutes use:
td.Triggers.Add(new TimeTrigger(DateTime.Now + TimeSpan.FromMinutes(30)));
Thank you dahal.
I've modified my code but still the same issue :(
I think the best way is to show the whole source ...
TaskDefinition td = ts.NewTask();
td.RegistrationInformation = "Reboot initiated by: " + ddlAgentFQDN.Items[i].Text;
td.Principal.LogonType = TaskLogonType.InteractiveToken;
td.RegistrationInfo.Author = HttpContext.Current.User.Identity.Name;

td.Settings.DisallowStartIfOnBattery = false;
td.Settings.DeleteExpiredTaskAfter = TimeSpan.FromSeconds(1);

if (ts.HighestSupportedVersion >= new Version(1,2))
{
td.Settings.RestartInterval = new TimeSpan(0,0,0);
td.Settings.RestartCount = 0;
}
td.Settings.WakeToRun = true;

TimeTrigger tt = new TimeTrigger(DateTime.Now + TimeSpan.FromMinutes(3));
DateTime executionEnd = DateTime.Now.AddMonths(1);
tt.EndBoundary = executionEnd;
String Notification = tbMMReason.Text + " Reboot initiated by User: "+HttpContext.Current.User.Identity.Name;
td.Triggers.Add(tt);
td.Actions.Add(new ExecAction("shutdown", " -r -f -t 240 -m \\\\" + ddlAgenFQDN.Items[i].Text + " -d P:0:0 -c \"" +Notification + "\"",null));
ts.RootFOlder.RegisterTaskDefinition(@"MMWebGen_" + ddlAgentFQDN.Items[i].Text,td,TaskCreation.CreateOrUpdate,"<username>","<password>",TaskLogonType.Password,null);
So the Task will be created but it will have the following trigger / values:
Name = MMWebgenRebot_System
Status= Ready
Triggers:At 2:22PM on 6/27/2013 - Trigger Expires at 7/27/2013 2:20:13PM
Next Runtime: 7/27/2013 2:20:14PM
Jun 27, 2013 at 2:31 PM
Hello dahal,

I'm so stupid .... OMG :(

It's working right now.
The "NextRunTime" will show the time when the Task will be removed.

Whatever ... thank you for your support.

Kind regards ....
Kai