InvalidOperationException in Process.WaitForExit

G

Guest

The requirement is to detect closing of a word document opened by a process.
There is some piece of code which has to be executed after the document is
closed.

Using Process.Start() document is opened, I used process.WaitForExit() to
stop further execution until the document is closed.

But once the document is closed, Process.WaitForExit() throws
“InvalidOperationException-No process is associated to the object†exception.

Even on using process.HasExited property/process.WaitForInputIdle(),same
exception is thrown.

This exception is thrown during Release as well as Debug mode.

Any idea why this exception occurs ?
 
D

Doug Semler

The requirement is to detect closing of a word document opened by a process.
There is some piece of code which has to be executed after the document is
closed.

Using Process.Start() document is opened, I used process.WaitForExit() to
stop further execution until the document is closed.

But once the document is closed, Process.WaitForExit() throws
"InvalidOperationException-No process is associated to the object" exception.

Even on using process.HasExited property/process.WaitForInputIdle(),same
exception is thrown.

This exception is thrown during Release as well as Debug mode.

Any idea why this exception occurs ?

I don't think you are waiting on the correct Process variable. Make
sure you aren't forgetting to assign the result of Process.Start() to
a variable of type Process.

Process p1 = new Process();
Process p2 = Process.Start("notepad.exe");

p1.WaitForExit(); <-- will throw
p2.WaitForExit(); <-- will not throw.

or....
p1.StartInfo = new ProcessStartInfo("notepad.exe");
p1.Start();
p1.WaitForExit(); <-- will not throw
 

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