If I look at Examine Running process the only functions which throw System.InvalidOperationException
with information process must exit before requested information
are ExitCode
and ExitTime
so you might have used these function of Process
class to find the information of running process and used any of the two functions in wrong way.
More probably you have this code,
Process p = ... Your way to find a process;//
p.Kill();
int exitCode = p.ExitCode; // Or you have ExitTime used
Please use WaitForExit
or HasExited
before accessing the ExitCode
or ExitTime
function something like,
Process p = ... Your way to find a process;//
p.Kill();
while(!p.HasExited)
{
p.WaitForExit(60000); //Wait for one minute
}
int exitCode = p.ExitCode; // Or you have ExitTime used
EDIT In MSDN documentation it is also clearly mentioned with section heading Note that,
The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.
EDIT After looking at your code do this,
Process Process = Process.Start(ProcessInfo);
while(!Process.HasExited)
Process.WaitForExit(Timeout);
//Finish.
int ExitCode = Process.ExitCode;
//Process.Close(); NO NEED AS PROCESS IS ALREADY EXITED
return ExitCode;