I am coding a feature in a program where users can edit documents stored in a database, it saves the document to a temporary folder then uses Process.Start to launch the document into the editing application, let’s say Microsoft Word for example.
Then my app needs to wait until they’ve closed the called process and replace the document in the database with the newly edited copy in the temp folder.
The following code works great as long as the called application isn’t already running:
ProcessStartInfo pInfo = new ProcessStartInfo(); pInfo.FileName=TempFolder + Path.DirectorySeparatorChar + f.Name; Process p = new Process(); p.StartInfo = pInfo; p.Start(); //p is null at this point if called application was already running //i.e. Microsoft Word is re-used instead of starting a fresh copy p.WaitForInputIdle(); p.WaitForExit();
Is there a way to force starting a completely new process or can anyone think of another way to handle this. I really don’t want the users to be able to do anything else in my app until they’ve closed the called process because I need to know if they edited that file or not at that point in time, not later when all sorts of other problems could creep up.
After further research and coming across a number of posts mentioning the unreliability of WaitForExit and the process’ Exited event, I’ve come up with a completely different solution: I start the process and don’t bother waiting for it, just pop up a modal dialog in which the user can click on update to update the temporary folder file back into the database when they’ve edited and saved the temporary file or cancel.
This way it’s in their hands and I don’t have to rely on the vagaries of Process.Start.
Thanks for everyone’s help.