Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my process's Exited method not being called?

Tags:

c#

.net

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 } 
like image 942
5YrsLaterDBA Avatar asked Dec 21 '10 21:12

5YrsLaterDBA


People also ask

How can you tell if a process is completed in C#?

Be sure you save the Process object if you use the static Process. Start() call (or create an instance with new ), and then either check the HasExited property, or subscribe to the Exited event, depending on your needs. You can use the Process. Start(string) method since it returns an instance of Process.

What is EnableRaisingEvents?

The EnableRaisingEvents property is used in asynchronous processing to notify your application that a process has exited.


1 Answers

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);  
like image 108
Elisha Avatar answered Oct 06 '22 00:10

Elisha