Solved

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

Posted on 2004-09-13
6
210 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video discusses moving either the default database or any database to a new volume.

760 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

18 Experts available now in Live!

Get 1:1 Help Now