I'm working on VS2015. I want to trigger to run my application after 20 seconds. How can i do it with Microsoft.Win32.TaskScheduler.dll? My code is as below:
void Actions::ScheduleRunApp()
{
TaskService^ ts = gcnew TaskService();
TaskDefinition^ td = ts->NewTask();
td->RegistrationInfo->Description = "Run LocalLockApp";
//td->Triggers->Add(gcnew DailyTrigger(2)); i want to run the application after 20 seconds instead
td->Actions->Add(gcnew ExecAction("notepad.exe", "C:\\test.log", nullptr));
ts->RootFolder->RegisterTaskDefinition(L"Test",td);
ts->RootFolder->DeleteTask(L"Test",true);
}
It looks like you have two applications, and you're trying to have application #1 run application #2, probably having application #1 exit before application #2 is started.
At first glance, I would say that using Task Scheduler for this is the wrong solution. Task Scheduler is for launching maintenance tasks on a regular basis, such as every day at 4:00 am. You could create a task that is triggered to run only once, at now + 20 seconds, but there's probably better ways of doing this.
In this case, I would do one of two things:
If you absolutely must use Task Scheduler, then I believe you want
TimeTrigger
instead ofDailyTrigger
. You also probably don't want to call DeleteTask before it has run: Instead, make sure the properties are set so that the task auto-deletes when it's not scheduled to run any more.