• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Console app: monitoring 16-bit process

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
skanade
Asked:
skanade
1 Solution
 
nigel5Commented:
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
 
nietodCommented:
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
 
nigel5Commented:
Won't the calling thread suspend until the termination of worker if you use WaitForSingleObject() ?? is this what you want?
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.

 
nietodCommented:
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
 
mnguyen021997Commented:
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
 
skanadeAuthor Commented:
ninetod, mnguyen,

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

Sanjay
0
 
nietodCommented:
The choice is yours.  You have to decide who was most instrumental in solving your problem.
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now