Link to home
Start Free TrialLog in
Avatar of Pra4444
Pra4444Flag for United States of America

asked on

checking the status of child process from parent without going on a wait loop

Hi,

I have an application A written in VC++ ...which is working fine...

On click of a button, I created a new process B from A using CreateProcess syntax.(so that both A and B can run simultaneously)
Now, i want to check the status of B and see whether it has finished executing or not..)

If i do this DWORD dwExitCode = 0;

do
{
   ::Sleep(100);

   if(!::GetExitCodeProcess(hHandle, &dwExitCode))
      // Error -> call 'GetLastError()'
}
while(dwExitCode == STILL_ACTIVE)

i can figure out when process B terminates..but the problem is A goes into a sort of a wait loop...i dont want to put A into a wait loop and users should be able to run application A as usual  till B finishes execution...ALso as soon as B finishes execution, A should be informed so that I can create a log file with some information such as time taken, username details..etc....

how do i do this??

Thanks,
Pra4444
Avatar of jkr
jkr
Flag of Germany image

Just use

WaitForSingleObject ( hHandle, INFINITE);

to wait until the process is finished.
Avatar of Pra4444

ASKER

Hi,

Thanks for your reply...
I have considered that also..but i dont want application A to wait for application B to finish.... If I use WaitForSIngleObject, Application A will have to wait till B is finished...

I want the users to perform any normal operation on Application A ...

regards,
Pra4444
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CmdrRickHunter
CmdrRickHunter

you can always WaitForsingleObject(object, 0) to check to see if the object is done (return code will vary depending on if it timed out - which it does instantly - or suceeded in the wait).  Just put it in your message loop.

What does A do?  If it has to do any work thats not caused by a message, MsgWaitForMultipleObjects will still lock up.  All it does is wait for either the objects, or for a windows message.  Chances are good that A is just running its message pump at this point, in which case the above solution will work just fine.  However, if you are running two processes that both need to do work, you would probably want to use the WaitForSingleObject with a timeout of zero to just "check up" on B
Avatar of Pra4444

ASKER

Hi,

I didnt understand both of your answers as i am fairly new to C++.....

I didnt understand this part of the code from jkr
while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &pi.hProcess,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }


can you explain???

This is what i have in my code right now
==========================
I am in APP A ::

//I create App B here..
app->ok = app->StartApp(); //StartApp() creates B and returns true or false
            if (app->ok)
            {
                  WaitForSingleObject(app->pi.hProcess, INFINITE );//currently i wait till i finish b and then write log...
                  app->WriteLog();
            }
Avatar of Pra4444

ASKER

Hi JKR,

Your code works the way i want ...thanks a lot....after reading about msgwaitformultipleobjects, this is what i understood.correct me if i am wrong..

What you have done is to do a wait on multiple objects, the objects being 1 for the new process....(is that right??)...so inside the loop, you just check whether any standard messages are being sent by any user through peekmessage..if so you calll dispatch message with the msg parameter.....and then it goes back to the waiting mode for the first process...

is this right??
Your code works perfectly!!!

can you also provide me some link about these topics in general???

Thanks,
Pra4444