Solved

Console app: monitoring 16-bit process

Posted on 1998-09-17
7
278 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 71
I could not build boost code, 10 87
Dialogbox API leak? 18 94
White board coding practice 3 87
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

785 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