0

I'm running this code snippet:

_taskListProcess = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C tasklist /FO CSV /NH";
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
_taskListProcess.StartInfo = startInfo;
_taskListProcess.EnableRaisingEvents = true;
_taskListProcess.Exited += new EventHandler(HandleTaskListProcessTerminated);
_taskListProcess.Start();

My issue is that the tasklist process never terminate. I see it dangling in the Window task manager. Therefore, my function HandleTaskListProcessTerminated is never called.

I'm developing on Unity which uses Mono.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Since you are redirecting the output, a buffer gets full. Try to take the proach mentioned here http://stackoverflow.com/q/439617/27083 to solve the issue. – tobsen Jul 23 '14 at 20:10

1 Answers1

0

The started process fills the StandardOutput buffer since you set RedirectStandardOutput = true;. Try to read from StandardOutput until you consumed everything:

_taskListProcess.Start();
while (!_taskListProcess.StandardOutput.EndOfStream)
{
    Console.WriteLine(_taskListProcess .StandardOutput.ReadLine());
}
tobsen
  • 5,328
  • 3
  • 34
  • 51