The title says it all. Application UI freezes when asynchronously reading output from console application. It freezes until i close the console application. What causes this issue?
UPDATE: My question was closed because it says that there are already solutions for this question. One of the answers does not actually answer my question: How do I redirect a console program's output to a text box in a thread safe way?
I have't put proc.WaitForExit();
anywhere in my code.
The second answer solved my issue: Cross-thread operation occurs when Console output is being redirected to textbox
Here is my code:
Process proc = new Process();
void beginSrvInst()
{
proc.EnableRaisingEvents = true;
proc.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
proc.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
proc.Exited += new System.EventHandler(ProcExited);
proc.StartInfo.FileName = srvInstDir + @"\consoleApp.exe";
proc.StartInfo.Arguments = InstCode;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.ErrorDialog = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.BeginErrorReadLine();
proc.BeginOutputReadLine(); }
void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
textBox2.Text += outLine.Data + Environment.NewLine; //write output to textbox
}
}
void ProcExited(object sender, EventArgs e)
{
resetControls();
}