Process started by Process.start() returns incorrect process ID?

52,140

Solution 1

An example of how I did it:

    bool started = false;
    var p = new Process();

    p.StartInfo.FileName = "notepad.exe";

    started = p.Start();

    try {
      var procId = p.Id;
      Console.WriteLine("ID: " + procId);
    }
    catch(InvalidOperationException)
    {
        started = false;
    }
    catch(Exception ex)
    {
        started = false;
    }

Otherwise, try using handles like this:
Using handlers
Getting handler

hWnd = (int) process.MainWindowHandle;
int processId;
GetWindowThreadProcessId(hWnd, out processId);

[DllImport("user32")]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);

Side note:
What happens if you get the array of process and iterate over them and compare the PIDs?

Process[] p = Process.GetProcessesByName( "testprogram" );
foreach(var proc in p)
    Console.WriteLine("Found: "+proc.Id == myExpectedProcId);

Solution 2

This:

using (Process process = Process.Start("notepad.exe"))
{
    process.WaitForInputIdle();
    Console.WriteLine(process.Id);
}

Actually works for me:

http://pasteboard.s3.amazonaws.com/images/1350293463417532.png

Task Manager:

http://pasteboard.s3.amazonaws.com/images/1350293536498959.png

My thoughts:

Actually your process starts another process and you are trying to get ID of some kind of launcher. (It can start itself by the way).

Solution 3

Below also returns the PID of a process

Process[] p = Process.GetProcessesByName("YourProcessName");

Now you can get process Id by using p[i].Id;

Share:
52,140

Related videos on Youtube

haynar
Author by

haynar

Updated on July 07, 2021

Comments

  • haynar
    haynar almost 3 years

    I am starting an executable using this code:

    Process proc = new Process();
    proc.StartInfo.FileName = executablePath;
    proc.Start();
    proc.WaitForInputIdle();
    

    after this calling proc.Id it gives me some integer, which is not real process ID. In the task manager there is another ID for this process and also I am using MS UI Automation to access this application, which also returns the same ID as in task manager. So my question is how can I get the real process ID of started process?

    UPDATE

    I found out that on Windows 7 it works fine and returns me the right ID, but not on Windows XP. What can be the reason?

    SCENARIO

    The scenario of the application is the following. I have a running embedded HTTP server, which is implemented not by me, (here is the source). The client connects to the web server and sends a request to run a program. In the request handler of my server I am just using Process.start() to start the requested application. As a web server the program creates threads for every client session connected to it (I assume so, as I didn't wrote it). Can this somehow help to identify the problem as it exists only on Windows XP X86 Service Pack 3?

    • Petr Abdulin
      Petr Abdulin over 11 years
      Actually proc.Id should give you valid PID for the process. Overwise it's a bug in framework.
  • haynar
    haynar over 11 years
    this doesn't solve my problem, as I don't know the index in the list of processes with the same name
  • Marcus
    Marcus over 11 years
    Got you, see edit. ID should give you the real PID and in the case above it does. If you don´t get the right PID, have you confirmed that the process is started and that it did not restart itself upon start?
  • haynar
    haynar over 11 years
    no it doesn't, because it gives me the same property, which contains another ID (I don't know what and why). Maybe there is a way to find the real ID using this "fake" one?
  • Marcus
    Marcus over 11 years
    Try using process handlers as described in the links.
  • haynar
    haynar over 11 years
    I don't know what is the process name, I am running calc.exe, but passing it as parameter returns me an empty array and I can't get proc.ProcessName it throws an exception: System.InvalidOperationException
  • Marcus
    Marcus over 11 years
    Calc.exe has a window handler so try using the code provided to get the process by window handle.
  • haynar
    haynar over 11 years
    proc.MainWindowHandle also throws the same System.InvalidOperationException exception
  • haynar
    haynar over 11 years
    but I just use Process.Start() as any other people, why it starts some kind of launcher?
  • haynar
    haynar over 11 years
    I am calling this code from a thread, can this be the reason?
  • AgentFire
    AgentFire over 11 years
    @haynar I am telling you, my guess is the issue is in your program. Try launching calc or something familiar to you.
  • haynar
    haynar over 11 years
    I am not familiar with .NET and I really don't understand the underlying architecture, so I can't find out the reason... I am trying to think logically, but with the lack of knowledge it doesn't solve my problem
  • Marcus
    Marcus over 11 years
    What is the exception message when trying to get the process by name?
  • AgentFire
    AgentFire over 11 years
    Try GetProcessById by that fake ID u receive and see what is returned.
  • Marcus
    Marcus over 11 years
    Try explicitly creating the StartInfo object and assign it to your process' StartInfo property instead of setting value by value.
  • haynar
    haynar over 11 years
    I found out that on Windows 7 it returns me the right ID but on the XP this fake one, I don't know what is the reason, but I will continue on win 7. Thank you very much for your support
  • haynar
    haynar over 11 years
    thank you for trying to help me, but this is not the case. I am running calc.exe and only one instance is being opened, so one instance, one real PID and one "fake" PID
  • Francesco Baruchelli
    Francesco Baruchelli over 11 years
    I'm using XP and everything works fine. Can you show us the code with the Thread?
  • haynar
    haynar over 11 years
    I am not sure if I can show it, because the scenario is a little bit complicated and a part of the code is written not by me so I am not sure what is going on in that part, I will try to explain it in the question
  • haynar
    haynar over 11 years
    I have updated my question again to include the scenario, which can help

Related