Solved

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

Posted on 2004-09-13
6
230 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:Pra4444
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12048779
Just use

WaitForSingleObject ( hHandle, INFINITE);

to wait until the process is finished.
0
 
LVL 12

Author Comment

by:Pra4444
ID: 12048815
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 12049412
Then, try

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &pi.hProcess,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  (   dwCode);
}


This will keep the message pump running
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 3

Expert Comment

by:CmdrRickHunter
ID: 12049678
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
0
 
LVL 12

Author Comment

by:Pra4444
ID: 12054910
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();
            }
0
 
LVL 12

Author Comment

by:Pra4444
ID: 12056189
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now