finding if a process is running or not
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: finding if a process is running or not

  1. #1
    Join Date
    Dec 2002
    Location
    Seattle
    Posts
    4

    finding if a process is running or not

    Hi all,

    In my MFC Aplication I have started a new process using
    "Create Process" function . The process starts up and runs ,
    but how do I determine if that process is still running ?

    I came across the function

    BOOL GetProcessTimes(
    HANDLE hProcess,
    LPFILETIME lpCreationTime,
    LPFILETIME lpExitTime,
    LPFILETIME lpKernelTime,
    LPFILETIME lpUserTime
    );

    In above function's arguments,
    "lpExitTime " is "Pointer to a FILETIME structure that receives the exit time of the process. " But MSDN library also says that if the process has not exited, the content of this structure is undefined.

    So , I am stuck . Any help ?

    Thanks,
    Ashwini

  2. #2
    Join Date
    Sep 2002
    Posts
    77
    After creating the process call WaitForSingleObject and pass the handle of the new process as first parameter.
    WaitForSingleObject will block your current thread / will not return until the process terminates.
    If you do not want to block your current thread create a new one where you call WaitForSingleObject.

  3. #3
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747

    some more info

    You can use GetExitCodeProcess to find the state of a process if you do not want to wait for the process to end but just check up on it. It will give you a STILL_ACTIVE notifier if the process is still running.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: some more info

    Originally posted by galathaea
    You can use GetExitCodeProcess to find the state of a process if you do not want to wait for the process to end but just check up on it. It will give you a STILL_ACTIVE notifier if the process is still running.
    And to add a small sample for this...
    Code:
    DWORD dwExitCode = 0;
    
    do
    {
      ::Sleep(100);
    
      if(!::GetExitCodeProcess(hHandle, &dwExitCode))
        // Error -> call 'GetLastError()'
    }
    while(dwExitCode == STILL_ACTIVE)
    
    // Process has terminated
    // 'dwExitCode' contains the exit code
    'hHandle' is returned within the process information structure which is passed to 'CreateProcess()'....
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  6. #6
    Join Date
    Dec 2003
    Posts
    5

    Re: some more info

    Originally posted by galathaea
    You can use GetExitCodeProcess to find the state of a process if you do not want to wait for the process to end but just check up on it. It will give you a STILL_ACTIVE notifier if the process is still running.
    But what if I want more information? I cannot believe that the process cannot tell me if the thread is currently running, or is in a suspended state?

    Does windows not have a call, or something very similar to it, that if I give the function a pointer or handle to my thread, it can return to me somehow the status of the thread? Something like...

    Code:
    switch (::GetThreadStatus(m_thread))
    {
       case SUSPENDED:
          break;
    
       case RUNNING:
          break;
    
       case TERMINATED:
          break;
    
       default:
          break;
    }

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Re: some more info

    Originally posted by elim
    But what if I want more information? I cannot believe that the process cannot tell me if the thread is currently running, or is in a suspended state?
    As far as I know, there is no way of getting this information...in other words, I did not find a way yet using regular API etc.
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  8. #8
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    The native api can be used to obtain this information.

    NtQuerySystemInformation using SystemProcessessAndThreadsInformation enumeration, there is a THREAD_STATE enum inside of the SYSTEM_THREADS struct.

    let google be your guide as far as code usage or buy Windows NT/2000 Native API reference by Gary Nebbitt (if you still can somewhere) and it is all laid bare in there.

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Mick
    The native api can be used to obtain this information.
    Well...I could have guessed that on my own...it looks like am getting too old for the job...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  10. #10
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by Andreas Masur
    Well...I could have guessed that on my own...it looks like am getting too old for the job...
    Nah, maybe you were just distracted, happens to me all the time...but anyways guess I'll spiel for the OP....some simple things people always assume there is an API for but it's a little more complex, usually you have to hit the Native Api to accomplish this in a robust way eg: getting command line params in/from another process (CreateRemoteThread? bah too intrusive, and too restrictive) BUT since the native api is not necassarily documented by Microsoft it can be a maintaince issue if underlying structs, api's change between releases, so you have to keep that in mind. And yes I am aware they finally put up NtQuerySystemInformation....

    Thread State is ever changing, so a snap shot really tells you just that, that's the threads state at the moment, making coding decisions based upon it is not very wise, displaying informational things is fine. In future SDK's etc, they may return addition thread info from the tool hlp lib eg: THREADENTRY32.

    You could also use (and works well in a distrubuted env)

    The performance monitoring api's

    PdhXXX to get thread state.

    WMI, parsing the Win32_Thread calls (which contains state information) (or the performance classes as they are encapsualted as well)

    possible states for the thread under nt/2k (haven't looked for xp but it's doubtful another member has been added)..

    StateInitialized - (recognized by the microkernel)
    StateReady- (prepared to run on next available processor)
    StateRunning - Obvious
    StateStandby -about to run, only one thread may be in this state at a time)
    StateTerminated - Obviouus
    StateWait - (not ready for the processor, when ready, it will be rescheduled)
    StateTransition - (thread is waiting for resources other than the processor)
    StateUnknown - Obvious

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center