Can't wait for other process to finish

B

Brad

I want to start another process in code, wait for it to finish, then
continue. No problem, I do the following:

prcs := System.Diagnostics.Process.Start('E:\blah.txt');
if assigned(prcs) then begin
prcs.WaitForExit;
prcs.Close;
end;

This works fine if my default application for the given file type is not
already running. If it is running and it handles multiple open
documents, it will just open a new document in the same process in which
case prcs is not assigned (null) because it didn't actually start the
process. Then of course, I can't wait for the process to exit.

Is there any way to get a pointer/handle to the process in these cases?
 
C

Cole

Here's an idea, don't know if it will work. Check to see if the process is
running before hand by using Process.GetProcessesByName. If it is running
just use the Process object that you get back from GetProcessesByName to
wait, instead of the one you used to create the new document.

Cole
 
B

Brad

Cole said:
Here's an idea, don't know if it will work. Check to see if the process is
running before hand by using Process.GetProcessesByName. If it is running
just use the Process object that you get back from GetProcessesByName to
wait, instead of the one you used to create the new document.

I'm sure that would work, but now that I think of it, that's not the
behaviour I want. I want to create a new process. It seems that
Process.Start doesn't really start a new process, it's just a shell
exec. There doesn't seem to be any .net equivalent of CreateProcess
(when I call that API it starts a new process even when is already
open). I don't want to have to try to programmitically determine when
an application's sub doc is closed; better to just create a new process
and wait until that one is finished. I'll have to use PInvoke.
 

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