Is there a way to get at the Process ID for a process that has been created as a result of a scheduled task? As an example, I can use the following to register a task and have it launch immediately (on registration):
TaskDefinition taskDefinition = taskService.NewTask();
RegistrationTrigger trigger = new RegistrationTrigger();
ExecAction action = new ExecAction("notepad.exe", "", "c:\mydir");
TaskFolder folder = taskService.GetFolder("MyFolder");
Task task = folder.RegisterTaskDefinition("KrisTask", taskDefinition);
The task / TaskDefinition classes do not expose PIDs, which makes sense since they don't necessarily correspond to a running process. The RunningTask class exposes an enginePID, however this appears to be the PID for the taskeng.exe process
that spawned the process (notepad.exe in my example). Looking at the Task Scheduler GUI, the History for a Task contains a "Create Task Process" event which lists the correct process id:
instance "notepad.exe" with process ID 9888.
What is the best way, if any, to retrieve the process ID for the spawned process? For now, I am going to do a manual set difference of the running processes before and after the RegisterTaskDefinition() call (with some arbitrary delay that is hopefully
"long enough") and filter by the executable name. This should work for my specific case, however it will not be a long-term solution.