Process.HasExited

A

Alan T

I want to make sure my process is completed:

Process.Start();
bool finished = false;
while (!finsihed)
{
Thread.Sleep(1000);
if (Process.HasExited)
{
Thread.Sleep(1000);
finsihed = true;
}
}

Are these correct ?

The reason I inserted Thread.Sleep(1000) before exiting the loop is my
prcess is creating a text file and exporting data to this text file, so I
need to give time for the process to close the text file even it it
finished.


However, I have to run the above code 100 times (ie. to create 100 text
files and export data to 100 text files, I will open the text file after the
above code is executed each time for each text file), sometime I still got
the exception when I tried to open the textfile in my code:
The text file is being used by other process.

So it seems the process has not finished when I tried to open the text file
at run time.
 
M

Marc Gravell

..WaitForExit() is easier, but essentially this should work; some complex
apps (e.g. Word) can use IPC (etc) so that the original process just passes
the request to a second (already running) process - is this a possiblity?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top