Can't wait for other process to finish

  • Thread starter Thread starter Brad
  • Start date Start date
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?
 
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
 
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.
 
Back
Top