Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to use spawnl / cwait

101 views
Skip to first unread message

Chris Bordeman

unread,
Feb 23, 2000, 3:00:00 AM2/23/00
to
Greetings.

Win9x and NT 4.0
Borland C++ Builder 3.0
Application is 32-bit

I'm trying to spawn another program and wait until it is done. I am using
spawnl and cwait, but I can't seem to get it to work correctly.


When using:

spawnl (P_WAIT, "Adobe\\Setup.exe", "Adobe\\Setup.exe", NULL);

a second instance of the current (parent) process, NOT "Adobe\\Setup.exe" is
spawned; both are hung.


When using:

int childid = spawnl (P_NOWAIT, "subdir\\runme.exe", "subdir\\runme.exe",
NULL);
cwait(0, childid, WAIT_CHILD);

childid is a negative number (is that normal?), and cwait and the child
process hangs until I kill the parent process, at which point the child
process continues execution normally.

If I show a Message box:

int childid = spawnl (P_NOWAIT, "subdir\\runme.exe", "subdir\\runme.exe",
NULL);
ShowMessage(childid);
int r = cwait(0, childid, WAIT_CHILD);

cwait returns the childid (r==childid) IMMEDIATELY, though
"subdir\\runme.exe" continues to run in the background.

Am I using these funcs in just completely the wrong way or what? Is there
an alternative?

Thanks.

Chris B.

---------------------
Chris Bordeman
Software Developer
EMI

Bret Pehrson

unread,
Feb 23, 2000, 3:00:00 AM2/23/00
to
Under Win32, you can wait on process handles:

PROCESS_INFORMATION pi = { 0 };
CreateProcess(..., &pi);
WaitForSingleObject(pi.hProcess, INFINITE);

--
Bret Pehrson
R&D Manager
Steton Technology Group
peh...@nospam.steton.com
Remove nospam to send e-mail
*When replying via e-mail, please carbon newsgroup*
NOSPAM

Chris Bordeman

unread,
Feb 23, 2000, 3:00:00 AM2/23/00
to
Thans, Bret. I tried:

_PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
int created = CreateProcess(
fullpath, // pointer to name of executable module
NULL, // pointer to command line string
NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
0, // creation flags
0, // pointer to new environment block
startupdir, // pointer to current directory name
&si, // pointer to STARTUPINFO
&pi // pointer to PROCESS_INFORMATION
);
if (!created)
{
blah blah blah...
}
WaitForSingleObject(
pi.hProcess, // handle of object to wait for
INFINITE // time-out interval in milliseconds
);

and the app just hangs on the WaitForSingleObject, same behavior as before -
I kill the app or remove the WaitForSingleObject and the child process runs
fine.

I have no idea what is going on.

(win98 SE)

"Bret Pehrson" <peh...@nospam.steton.com> wrote in message
news:38B47B33...@nospam.steton.com...

Runu Knips

unread,
Feb 24, 2000, 3:00:00 AM2/24/00
to
Chris Bordeman schrieb:

I think you don't have initialized STARTUPINFO correctly:

[...]
STARTUPINFO start;
[...]
memset (&start, 0, sizeof(start));
start.cb = sizeof (STARTUPINFO);
[...]

Jussi Jumppanen

unread,
Feb 24, 2000, 3:00:00 AM2/24/00
to
In article <8923k...@enews2.newsguy.com>, "Chris Bordeman" <chr...@autoboatrv.net> wrote:

>and the app just hangs on the WaitForSingleObject, same behavior as before -
>I kill the app or remove the WaitForSingleObject and the child process runs
>fine.

If this is a Windows program then you can not just stop and wait for the
appliction unless the waiting code is in a thread. If the code is part of the
main windows application then you have to keep the message system
running.

This means you need code that looks something like this (pseudo
code only):

while (task is running running) {
if (PeekMessage()) {
GetMessage();
DispatchMessage();
}
Sleep(100);
}

This is a message pump. If you search deja.com you are bound to find
lots of proper examples of how to code these.

Jussi Jumppanen
Author of: Zeus for Windows, Win32 (Brief, WordStar, Emacs) Text Editor
"The C/C++, Java, HTML, Pascal, Cobol, Fortran programmer's text editor"
http://www.zeusedit.com

Chris Bordeman

unread,
Feb 24, 2000, 3:00:00 AM2/24/00
to
Hey, that works! I still have a problem, though - The childprocess ran and
terminated like it should, and my program continued afterwards, but the
child had spawned a _grandchild_. How do I wait for the child _and_ all
grandchildren?

Here is my code in it's current incarnation:

int created = CreateProcess(
fullpath, // pointer to name of executable module

NULL, // pointer to command line string, NULL == same


NULL, // pointer to process security attributes
NULL, // pointer to thread security attributes
FALSE, // handle inheritance flag
0, // creation flags
0, // pointer to new environment block

curdir, // pointer to current directory name


&si, // pointer to STARTUPINFO
&pi // pointer to PROCESS_INFORMATION
);

[...]

while (true)
{
int result;
result = WaitForSingleObject(pi.hProcess, 1);
if (result == WAIT_FAILED || result == WAIT_OBJECT_0)
break;
Application->ProcessMessages();
}

Thanks,
Chris B.

Andy Lutomirski

unread,
Feb 26, 2000, 3:00:00 AM2/26/00
to
"Chris Bordeman" <chr...@autoboatrv.net> wrote in message
news:893n7...@enews4.newsguy.com...

> while (true)
> {
> int result;
> result = WaitForSingleObject(pi.hProcess, 1);
> if (result == WAIT_FAILED || result == WAIT_OBJECT_0)
> break;
> Application->ProcessMessages();
> }

This is bad. You are polling. Use MsgWaitForMultipleObjects().

Andy


>
> Thanks,
> Chris B.
>


Andy

0 new messages