Click to See Complete Forum and Search --> : [RESOLVED] cmd.exe process not closing


tamaro
November 23rd, 2006, 08:15 AM
I use the following code to run an application which creates an output txt file. for some reason the cmd.exe process does not die - it stays in the task manager process list until I close my C# application.
How come??


Process p = new System.Diagnostics.Process();
System.IO.StreamWriter sw;
System.IO.StreamReader sr;

ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;

p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;

string strCubistCmd = "START /B CubistX -f input.txt > output.txt";

sw.AutoFlush = true;
sw.WriteLine("C:");
sw.WriteLine("CD \"C:\\Program Files\\Cubist\"");
sw.WriteLine("START /B CubistX -f input > output");

// wait till output file is created or timeout
int i = 0;
while (!File.Exists("output.txt") & i<10)
{
Thread.Sleep(1000);
i++;
}

p.CloseMainWindow(); // the process does not create a window, but i thought this might help...
p.Close();

riscutiavlad
November 23rd, 2006, 09:19 AM
I tested your code (well, something similar) and this is what I noticed: the process stays in the Task Manager until its object is collected by the Garbage Collector.

If you want it to disappear from the Processes list, as soon as your object (p) goes out of scope insert a GC.Collect(); line in your code. This will force the Garbage Collector to sweep the memory and it will remove the process.

tamaro
November 26th, 2006, 04:37 AM
thanks!