none
How to get the Process ID for the most recently opened window RRS feed

  • Question

  • Hi,
    I have an program that when multiple windows are opened, it will open as "Session A", Session B, Session C and so on..
    Here's my scenario:
    Sessions A, B, C are running and B completes its operation. So, now there's just A & C running. After a while another instance kicks in and now opens as Session B since that's not running. How do I get the Process ID for Session B? Since that's the most recent window opened? All their process names are same, so I have to go by Process ID.

    Thank you
    Abhi

    Wednesday, June 6, 2018 1:38 PM

Answers

  • Do you mean windows or processes?
    If processes, maybe
    Process.StartTime Property
    https://msdn.microsoft.com/en-us/library/system.diagnostics.process.starttime(v=vs.110).aspx
    meets your needs.

    With kind regards


    • Marked as answer by h_abhijith Thursday, June 7, 2018 5:24 PM
    Wednesday, June 6, 2018 5:02 PM

All replies

  • Why do you need the most recent?

    Process IDs are not sequential so you cannot assume PID 123 started before PID 456. When a process started is not part of the standard information that .NET has access to. You could get it by P/Invoke but I don't know why you'd want it.

    If it were me I would be keeping track of the processes that I cared about via their PID. When I wanted to "detect" new instances I'd get the running processes again and look for any that match the process name I'm looking for. Then I'd ensure that the new process(es) are in my PID list while removing the old ones.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 6, 2018 1:58 PM
    Moderator
  • Do you mean windows or processes?
    If processes, maybe
    Process.StartTime Property
    https://msdn.microsoft.com/en-us/library/system.diagnostics.process.starttime(v=vs.110).aspx
    meets your needs.

    With kind regards


    • Marked as answer by h_abhijith Thursday, June 7, 2018 5:24 PM
    Wednesday, June 6, 2018 5:02 PM
  • @MaybeCompletelyW, good catch, totally missed that property when I was looking at the members of Process.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 6, 2018 5:05 PM
    Moderator
  • Thank you Michael, I hadn't thought about this. But I think doing it by Process.StartTime is easier and it worked for me.
    Thursday, June 7, 2018 5:23 PM
  • Thank you @MaybeCompletelyW. I got it working by sorting the processes based on their StartTime and got the instance from it.
    Thursday, June 7, 2018 5:24 PM