Re: running external program, waiting for completion

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Bob Moore (bobm_at_mvps.org)
Date: 11/11/04


Date: Thu, 11 Nov 2004 16:53:44 +0000

On Wed, 10 Nov 2004 15:59:19 -0000, "Steve Kearon"
<anonymous@discussions.microsoft.com> wrote:

>I'm trying to start an external utility from a C++ program, but I'm having
>some problems
>I've tried various approaches, but all seem to suffer from the same problem:
>I can start the utility but if I try to wait for it to exit, then I'll wait
>forever (& the utility never seems to start running). One example:

snipped from my website:

**********
The problem arises because your application has a window but isn't
pumping messages. If the spawned application invokes SendMessage with
one of the broadcast targets (HWND_BROADCAST or HWND_TOPMOST), then
the SendMessage won't return to the new application until all
applications have handled the message - but your app can't handle the
message because it isn't pumping messages.... so the new app locks up,
so your wait never succeeds.... DEADLOCK.

If you have absolute control over the spawned application, then there
are measures you can take, such as using SendMessageTimeout rather
than SendMessage (e.g. for DDE initiations, if anybody is still using
that). But there are situations which cause implicit SendMessage
broadcasts over which you have no control, such as using the
SetSysColors API for instance.
**********

Your options are :

1. Split the wait into another thread
2. timeout on the wait and use PeekMessage in the wait loop (ugh)
3. Use the MsgWaitForMultipleObjects API

Bob Moore
http://bobmoore.mvps.org/