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

Saint
  • 5,397
  • 22
  • 63
  • 107
  • http://stackoverflow.com/questions/2316596/system-diaganostics-process-id-isnt-the-same-process-id-shown-in-task-manger-wh – Jason Evans May 31 '11 at 08:17
  • do you mean `Process p = Process.Start(psi);` ? – Paolo Falabella May 31 '11 at 08:20
  • To clarify, is BatchFile an actual executable or is it a batch-file in the "normal" sense (i.e. a script which calls other executables, one of which is the one you're interested in?). If it's the former, then it should be as you expect. However, if it's the latter, then the process id will be that of the process executing the script, which then kicks off the program of interest, which will have a different id. – cristobalito May 31 '11 at 08:22

3 Answers3

2

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.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
2

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

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • No, ID in TaskManager is completely different and do not fit into any process in TaskManager – Saint May 31 '11 at 08:24
  • @Saint_pl - if the batch file just starts another exe then it's process won't last long so you might not see it the Task Manager. – ChrisF May 31 '11 at 08:25
  • If I understand correctly - p.ID is id of running batchfile (e.g. "start.bat") ? Not this program that is run from inside of this start.bat? – Saint May 31 '11 at 08:31
  • @ChrisF - But if I use http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx as you proposed I still get different p.ID that that in TaskManager – Saint May 31 '11 at 08:43
  • @Marc Gravell - Process p = Process.Start(@"C:\Program Files\MyProgram.exe", @"abc -i jouFile.jou"); – Saint May 31 '11 at 09:38
1

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
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Accurate observation about HasExited. But finally how can I run external program and to catch its id? – Saint May 31 '11 at 11:27
  • @Saint_pl if your launcher is exiting, then you'll have to find it the hard way... or: don't use the launcher (some apps allow you to add switches to say "don't do that", but that is entirely usage-specific) – Marc Gravell May 31 '11 at 11:33
  • Solution is CommandLine and specyfic name of the file *.jou to recognize the running process – Saint Jul 15 '11 at 15:47