How to auto-delete a task after running once?

Nov 7, 2012 at 4:49 PM

I want to create a task that does the following:

- Run once immediately

- Self-delete task after running

How to implement this? I looked around the documentation, but couldn't find one. Appreciate any pointers.

Coordinator
Nov 8, 2012 at 4:23 AM
TaskDefinition td = ts.NewTask();
// Set the TimeSpan to a value that gives the action long enough to complete
td.Settings.DeleteExpiredTaskAfter = TimeSpan.FromMinutes(1);
td.Triggers.Add(new TimeTrigger());
// Set the action to what needs to execute
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
ts.RootFolder.RegisterTaskDefinition("TaskName", td);

Nov 8, 2012 at 5:11 PM
Edited Nov 8, 2012 at 5:26 PM
dahall wrote:
TaskDefinition td = ts.NewTask();
// Set the TimeSpan to a value that gives the action long enough to complete
td.Settings.DeleteExpiredTaskAfter = TimeSpan.FromMinutes(1);
td.Triggers.Add(new TimeTrigger());
// Set the action to what needs to execute
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
ts.RootFolder.RegisterTaskDefinition("TaskName", td);

I got an error with message (37,4):EndBoundary.

Adding StartBoundary and EndBoundary fixed the error.

In your code, you are setting DeleteExpiredTaskAfter value to a number. In my case, i don't want to restrict by a duration. I want to let the task run to completion and get deleted after that. Is this possible?

Many thanks!

Coordinator
Nov 9, 2012 at 2:58 AM

My bad. DeleteExpiredTaskAfter requires the EndBoundary to be set. StartBoundary is set automatically in the TimeTrigger constructor to DateTime.Now.

I don't know of anyway to delete the task immediately upon completion other than have the task's action delete the task or wait for the DeleteExpiredTaskAfter to kick in.