Solved

Console app: monitoring 16-bit process

Posted on 1998-09-17
7
276 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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