Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Starting programs

I'd like to know how to start a program and have my code not continue until the called program exits. When I use CreateProcess or WinExec they spawn threads and execute along side my app. Any suggestions?
0
forloop
Asked:
forloop
  • 10
  • 9
  • 7
  • +3
1 Solution
 
arbitraryCommented:
you can use a While loop on the HANDLE you receive from CreateProcess with a sleep(dwMSeconds) inside.

0
 
jkrCommented:
>>you can use a While loop on the HANDLE you receive from
>>CreateProcess with a sleep(dwMSeconds) inside.

OUCH OUCH OUCH!!!

The proper way is to use the process handle returned in the PROCESS_INFORMATION structure filled in by 'CreateProcess()' in a call to 'WaitForSingleObject()'...

arbitrary, what you're suggesting means _polling_ for an object's state in a multiprocess environment - that's not what the whole thing was designed for!
0
 
nietodCommented:
I agree with jkr, except I don't think "ouch" is adequate.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
forloopAuthor Commented:
jkr, you deserve the points here if you want them.
0
 
forloopAuthor Commented:
When I use the WaitForSingleObject w/ INFINITE as the wait value, the installation program I'm calling (ISYS) locks up at the point where it creates it's program groups. If I kill my program, ISYS continues to install. Here's my code:

// BEGIN CODE

STARTUPINFO infoStart;
PROCESS_INFORMATION infoProcess;
CString Temp;

memset(&infoStart,0,sizeof(infoStart));
infoStart.cb=sizeof(STARTUPINFO);
Temp.Format("%s\\isysrun\\setup.exe",m_Drive);
CreateProcess(NULL,(char *)(LPCTSTR)Temp,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&infoStart,&infoProcess);
WaitForSingleObject(infoProcess.hProcess,INFINITE);

// END CODE

I can set the wait value to something other than INFINITE, but that defeats the purpose (Waiting for the setup to end before continuing). I'd rather use a Sleep statement.

This could just be an issue with this particular program, but I thought I'd bring out the code and see if anyone notices any problems.
0
 
nietodCommented:
I don't see any problems there.  What is the "context" of this code?  i.e. when is it called.  Could the OS be locked up waiting for this to return ion some way?  For example, if you were to do this inside the DLLMain procedure of a DLL it would cause a lock up.

Also note that the two handles returned in InfoProcess must be closed eventually.  
0
 
jkrCommented:
I agree with nietod - this code certainly doesn't cause the problem - maybe that the setup program you're calling is opening a dialog in the background?
Another thing - perhaps you should set the working directory of the setup program to the one it is started from. This would prevent errors that result of the program being unable to locate files it searches for.
Try:
/ BEGIN CODE

STARTUPINFO infoStart;
PROCESS_INFORMATION infoProcess;
CString Temp;
CString strDir;

memset(&infoStart,0,sizeof(infoStart));
infoStart.cb=sizeof(STARTUPINFO);
Temp.Format("%s\\isysrun\\setup.exe",m_Drive);
strDir.Format("%s\\isysrun\\",m_Drive);
CreateProcess(NULL,(char *)(LPCTSTR)Temp,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,(char*) (LPCTSTR) strDir,&infoStart,&infoProcess);
WaitForSingleObject(infoProcess.hProcess,INFINITE);

// END CODE

BTW:
nietod - 'ouch' may not be adequate, but that's simply how i felt ;-)
0
 
jkrCommented:
>>jkr, you deserve the points here if you want them.

Thanks, but let's get this app running first ;-)
0
 
forloopAuthor Commented:
The code is ran in an MFC executable (not a DLL). The CreateProcess call is fine (with or without a working dir). I can change the word INFINITE to 30000 in the above code and ISYS never locks up, but after 30000 ms my program begins executing more code. ISYS only freezes when I set it to INFINITE. It must just be some weird problem with this particular program.
0
 
nietodCommented:
??? When you specify INFINITE, Is the program that you are using CreateProcess() for running?  If so does it end running?  This use of WaitForSingleObject() is to hold up your program until the program it starts is done.  So does that other program get done?  
0
 
erick1217Commented:
try one of the _wspawn function with the _P_WAIT argument
0
 
forloopAuthor Commented:
nietod: yes.. the called program is running.. in fact it and my program show "not responding" in the task manager.
0
 
jkrCommented:
'Not responding' is because the task manager uses a call to 'SendMessageTimeout()' to determine whether a program can handle requests within a predefined perion (5s). As your program is blocked at 'WaitForSingleObject()', this isn't alarming at all - that's normal ;-)
I'd suggest starting a simple test application that does nothig but calling 'Sleep()'...
0
 
nietodCommented:
That expains why the waiting program is not responding.  Why isn' the applicatin that is started, responding?  And most importantly, does this program ever get done?  Your first program SHOULD be locked up until the program its runs gets finished.  So if the program it runs doesn't finish, it will stary locked up forever.   (And remember--this is what you say you want.  It is not a bad thing, as long as that other program finishes quickly.)
0
 
jkrCommented:
Oops, i didn't realize that the spawned program is 'not responding', too - does it run when started 'standalone'?
Note that when this program never finishes, your application will block at 'WaitForSingleObject()' at the predefined timeout...
0
 
forloopAuthor Commented:
The called program runs totally fine if:

1. Called standalone
2. Called by CreateProcess and WaitForSingleObject is set it a value other than INFINITE.

Otherwise, if any thing is waiting (INFINITELY) for the task to finish, the program will freeze at a particular point.

This goes for a simple FindWindow loop on the called process.

0
 
nietodCommented:
Which program is freezing?  The on that did the CreateProcess or the other one?  

These two processes don't have to communicate in any way do they?  They don't send messages back and forth or share mutexes or other OS objects do they?
0
 
forloopAuthor Commented:
The program being called by CreateProcess from my program. My program is basically a simple installer that calls another installer when required. They do not communicate with each other at all.. My installer basically copies a few files, calls CreateProcess on the 2nd installer (the one that freezes) and then runs the installed program after its done.
0
 
nietodCommented:
Is it freezing at the same place every time or different places?  If the same place, what is it doing there?
0
 
forloopAuthor Commented:
same place. it has a message box up that's saying "creating program groups"
0
 
nietodCommented:
It sounds like this program is not one you wrote, Is that the case?  If so, that makes things considerably tougher.  If you did write it, what place IN THE SOURCE CODE does it stop at.
0
 
jkrCommented:
Hmm - just an (intermediate) idea to check for the reasons:

try

while ( WAIT_TIMEOUT == WaitForSingleObject(infoProcess.hProcess,10000));

This is ugly and shouldn't be the final solution, and to be honest, i'm stumped if this works...
0
 
superole2Commented:
Have you tried using the "system()" fuction?

Syntax

#include <stdlib.h>
int system(const char *command);

Description

Issues an operating system command.
system invokes the operating system command processor to execute an operating system command, batch
file, or other program named by the string command, from inside an executing C program.
To be located and executed, the program must be in the current directory or in one of the directories listed in
the PATH string in the environment.
The COMSPEC environment variable is used to find the command processor program file, so that file need not
be in the current directory.

Return Value

If command is a NULL pointer, system returns nonzero if a command processor is available.
If command is not a NULL pointer, system returns 0 if the command processor was successfully started.
If an error occurred, a -1 is returned and errno is set to one of the following:

ENOENT      Path or file function not found
ENOEXEC      Exec format error
ENOMEM      Not enough memory


0
 
jkrCommented:
superole2 - did you read the question history?????
You're far away from that what we already discussed, and your answer isn't matching the requirements at all :-(

faster - BTW, what's going on? ;-)
0
 
forloopAuthor Commented:
Well, it's just an isolated thing.. I've called many other programs using the techniques you guys have suggested and they all work fine. This particular app does like it.. So.. Thanks for your help everyone and I'll have to live with my work-around on this project.
0
 
jkrCommented:
So - do you think i should lock the Q? ;-)
0
 
forloopAuthor Commented:
yeah.. lock it.
0
 
jkrCommented:
Thanx ;-) !
(let's see that we get that DCOM thing running <s>
0
 
forloopAuthor Commented:
Thanks again for the help on this one!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 10
  • 9
  • 7
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now