Different reference returned from Process.Start
ProcessStartInfo psi = new ProcessStartInfo(BatchFile)
Process p = Process.Start(psi)
Why p.ID
开发者_如何学运维is different than process id visible in WindowsTaskManager
(BatchFile
is path to external program with appropriate parameters)
I would assume that it's because p.ID
is the id of the process that's running the batch file rather than the id of the process started by the batch file.
You can start the executable directly by Process.Start
by using the correct overload
I assume BatchFile is some kind of cmd or bat file that runs other processes one by one.
So in Windows Task Manager you actually see ids of those processes that are run by batch file.
Examples
If I do this
var p = Process.Start("notepad.exe");
p.Id
will match to the PID
from Task Manager.
However, if I do this:
var p = Process.Start("test.cmd"); // test.cmd has notepad.exe call inside
p.Id
will be different from PID
shown in the Task Manager.
A process ID is only meaningful while the process is alive. The first thing to check is .HasExited
- if this is true, ignore the process ID; it no longer has any meaning.
There are a number of ways you can start something and have no process left even though you can apparently see it still on screen:
- if it is a script/bat/cmd that spawns something and exits (remember: you are watching the script, not the "something")
- if the exe does some inter-exe voodoo internally - for example, most of the office apps and internet explorer do this; if there is an existing process, it forwards the args to that process to handle, and exits immediately
精彩评论