Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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?

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,

Posted

this is wha I do. This is in VB but easy to translate I am sure for C

put after the pApp.start

   Do While Not pApp.HasExited
           Application.DoEvents()
   Loop

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
  • Leaders
Posted
this is wha I do. This is in VB but easy to translate I am sure for C

put after the pApp.start

   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.

[sIGPIC]e[/sIGPIC]
Posted
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
Nothing is as illusive as 'the last bug'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...