Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Console app: monitoring 16-bit process

Posted on 1998-09-17
7
Medium Priority
?
284 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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