Solved

Console app: monitoring 16-bit process

Posted on 1998-09-17
7
280 Views
Last Modified: 2010-04-01
I have a console app from which I am starting a 16-bit program, PKZIP and I want to wait for the program to finish execution before proceeding. I have the following code:

  CreateProcess(NULL, combuf, NULL, NULL, FALSE, 0 ,      NULL, NULL, &si, &pi);
  DWORD code=STILL_ACTIVE;
  while (code==STILL_ACTIVE) {
     Sleep(1000);
     if (!GetExitCodeProcess(pi.hProcess, &code)) {
       fprintf(stderr, "ERROR: Can't find process            created.\n");
       return 1;
     }
   } //while
  Sleep(2000);

This doesn't work for some reason. The PKZIP is started but doesn't do its job right (producing a list file). If I vary the ending sleep to about 10 secs, it works. Is there some better code to wait for a thread? Also, any other comments are welcome.

Thanks,
Sanjay
0
Comment
Question by:skanade
7 Comments
 

Expert Comment

by:nigel5
ID: 1173014
Have you tried running a 32 bit app this way. I have a watch dog process monitoring several applications it start, but they were all compiled on the same machine as 32 bit apps. If you want, I can supply code Monday.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173015
What a second.  If the sleep is too short, it causes the prosses started to run differently?  There is nothing in the code for this process that could be effecting the other process.  At least nothing you've shown.  That doesn't make sense.  What comes after this code?

I wonder if there might be a problem with using GetExitCodeProcess() this way.  What does it return if the process hasn't started? Perhaps if the sleep() is too short, the process hasn't started and you are fooled into thinking it is done....

The best way to determine if a process is finished is to use WaitForSingleObject().  (You can avoid the sleep that way as well.)  This is much more efficient for everyone involved.
0
 

Expert Comment

by:nigel5
ID: 1173016
Won't the calling thread suspend until the termination of worker if you use WaitForSingleObject() ?? is this what you want?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:nietod
ID: 1173017
Yes, WaitforSingleObject() will cause the calling thread to suspend until the timout expires (unless it is infinite) or until the other process is finished.  
0
 
LVL 2

Accepted Solution

by:
mnguyen021997 earned 50 total points
ID: 1173018
a few things:

indeed waitforsingleobject() is the way to go with the resulting process handle (gotten from createprocess()) -- make sure you close the process handle when done to avoid leaks.

since this is a 16 bit app, launch the app in a separate VDM (there is a flag for this in createprocess).  if not, NT keeps around a default VDM (first one instantiated) as a caching mechanism so it may never shutdown if your process is the first 16 bit process created.
0
 

Author Comment

by:skanade
ID: 1173019
ninetod, mnguyen,

Thanks! WaitForSingleObject works. Now, whom should I award the points? Ninetod replied first. Please advise.

Sanjay
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173020
The choice is yours.  You have to decide who was most instrumental in solving your problem.
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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

828 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