Reputation: 13068
Here is the use case: - Create a new win task, run immediately and once complete, delete the task.
Here is basic code to create a task using C#.
using (TaskService ts = new TaskService(null))
{
string projectName = "runnowtest" + Guid.NewGuid().ToString();
//create new task
TaskDefinition td = ts.NewTask();
Trigger mt = null;
//setup task as Registration trigger
mt = td.Triggers.AddNew(TaskTriggerType.Registration);
mt.StartBoundary = DateTime.Now;
//delete the task 1 minute after the program ends
td.Settings.DeleteExpiredTaskAfter = new TimeSpan(0, 1, 0);
//run the notepad++ in the task
td.Actions.Add(new ExecAction("notepad.exe"));
//register task
Task output = ts.RootFolder.RegisterTaskDefinition(projectName, td);
//check output
Console.WriteLine(output != null ? "Task created" : "Task not created");
}
The API doesn't seem to have a property/flag to mark task as run once. I am trying to ensure the above task runs only once and deletes immediately after that. Any thoughts are much appreciated!
Upvotes: 2
Views: 5134
Reputation: 841
You have a collection running tasks and you can end them using the Stop() method.
I did in the OnStop method to clean up created tasks, just take the same logic and include it where needed.
protected override void OnStop()
{
TaskService ts = new TaskService();
var tasks = ts.GetRunningTasks();
foreach(var t in tasks)
{
t.Stop();
}
}
Upvotes: 0
Reputation: 31
I know its old but for reference... To use DeleteExpiredTaskAfter you need to give trigger EndBoundary:
var trigger = new RegistrationTrigger{Delay = TimeSpan.FromSeconds(5), EndBoundary = DateTime.Now.Add(TimeSpan.FromSeconds(50))};
Upvotes: 3
Reputation: 35613
You can set the task XML definition to set all the things not exposed directly by the API.
Upvotes: 0