private void myProcess_Exited(object sender, System.EventArgs e)
{
while (sr.Peek() != -1)
output += sr.ReadLine() + “\n”;
eventHandled = true;
}

ExePath = “C:\a.exe”;
Process p = new Process();

ProcessStartInfo psI = new ProcessStartInfo(ExePath);
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(this.myProcess_Exited);
p.Start();

p.MaxWorkingSet = (IntPtr)(1024 * 1024 * 32); // memory set

sw = p.StandardInput;
sr = p.StandardOutput;
err = p.StandardError;

string input = “sample input”;
long limit = 5 * 1000; // 5 sec max time limit set for the exe
output = “”;

sw.Write(input);
sw.Close();

int id = p.Id;

eventHandled = false;

const int SLEEP_AMOUNT = 100;

while (!p.HasExited)
{
start += SLEEP_AMOUNT;
if (start > limit)
{
break;
}
Thread.Sleep(SLEEP_AMOUNT);
p.Refresh();
}

now we will get the output in the output variable

Advertisements