Why is my process's Exited method not being called?
I have following code, but why is the ProcessExited
method never called? It is the same if I don't a use Windows shell (startInfo.UseShellExecute = false
).
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);
try
{
Process correctionProcess = Process.Start(startInfo);
correctionProcess.Exited += new EventHandler(ProcessExited);
correctionProcess.WaitForExit();
status = true;
}
.....
internal void ProcessExited(object sender, System.EventArgs e)
{
//print out here
}
Solution 1:
In order to receive a callback on Exited
event, the EnableRaisingEvents
must be set to true.
Process correctionProcess = Process.Start(startInfo);
correctionProcess.EnableRaisingEvents = true;
correctionProcess.Exited += new EventHandler(ProcessExited);
Solution 2:
From MSDN:
The Exited event indicates that the associated process exited. This occurrence means either that the process terminated (aborted) or successfully closed. This event can occur only if the value of the EnableRaisingEvents property is true.
Have you set that property to true?
Solution 3:
You must set Process.EnableRaisingEvents
to true
.
Solution 4:
Set correctionProcess.EnableRaisingEvents = true