Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-13
6
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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