Trigger an application using Microsoft.Win32.TaskScheduler.dll

558 Views Asked by At

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);
}
1

There are 1 best solutions below

5
On BEST ANSWER

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:

  • Keep application #1 running. Sleep for 20 seconds, and then run application #2. Hide your window if the user has requested application exit.
  • Create a helper application. The job of this helper application is to delay 20 seconds and then run application #2. Have application #1 launch the helper application, then application #1 is free to exit. You could even do this with a batch file if you don't want to have another .exe file hanging around.

If you absolutely must use Task Scheduler, then I believe you want TimeTrigger instead of DailyTrigger. 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.