Solved

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

Posted on 2004-09-13
6
239 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 101
matchUp  challenge 9 115
Annoying "thing" blocks my view 4 115
unix example issues 18 98
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.

840 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