Waiting for a Process to Finish before continuing [VC#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
At a certain point in my program I need to launch a program [oApp.exe] with a specific command line parameter.
To accomplish this I used the Process class as shown in the code below:

Code:
Process.Start("oApp.exe", "c:\\text.txt");

This will launch oApp.exe (with command line parameter C:\Text.txt) which should take 5-10mins to process.
Problem is, I want my code to WAIT for this process to complete/finsish (the 5-10mins) before going forward seeing as the next line of code assumes that the Text.txt was processed.
The way it is now, the process is launched and them my code continues (it does not wait) which consequently causes a huge amount of fatal errors further down.

So, how do I WAIT for my Process to complete? Do I have to actually make a Process variable?
Code:
Process pApp = new Process();
pApp.StartInfo.FileName = "oApp.exe";
pApp.StartInfo.Arguments = "C:\\text.txt";
pApp.Start();

But that also doesn't WAIT - however I assume it gives us more control....
Any help/hints would be much appreciated. Thanks,
 
this is wha I do. This is in VB but easy to translate I am sure for C
put after the pApp.start
Code:
    Do While Not pApp.HasExited
            Application.DoEvents()
    Loop
 
techmanbd said:
this is wha I do. This is in VB but easy to translate I am sure for C
put after the pApp.start
Code:
    Do While Not pApp.HasExited
            Application.DoEvents()
    Loop
One drawback with this method is that all of the spare CPU power is spent on your application calling DoEvents(). The CPU will be made available when other applications need it, but the end user might notice some unusual behavior. If you look in the task manager you will see CPU usage jump to 100%. If the user has a variable speed CPU fan, he will hear it revving up although to him there is no appearent intense use of the CPU.
 
Third way would be to add an event handler to the Exited event on the process. However you have to set the EnableRaisingEvents property to true for the event to work
 
Back
Top