0

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();
    }
  • Where are you hanging? You did not show where you wait for the process to exit. Can you add this code? – Alois Kraus Apr 04 '21 at 18:38
  • You're not setting the Synchronizing object anywhere (and the Process is declared somewhere else, so it can be used just once). Your `OutputHandler` should throw when you touch `textBox2`. Where is the Error stream output handled? You also don't set `EnableRaisingEvents`, so the Process just goes off duty without notice and it's not disposed. – Jimi Apr 04 '21 at 18:42
  • @AloisKraus i updated the code – Smoking Diesel GR Apr 04 '21 at 18:43
  • @Jimi i've set enableRaisingEvents to true – Smoking Diesel GR Apr 04 '21 at 18:45
  • Your code has at least one problem, possibly two. The first is that you don't do anything to make sure controls are updated in the UI thread. You can fix this using `Control.Invoke()`. From your description, it's also possible (likely) that you are calling `WaitForExit()` somewhere, deadlocking the code. See duplicates for how to fix both of these issues. If you need more help, post a new question in which you have provided a proper [mcve] that reliably reproduces the problem. – Peter Duniho Apr 04 '21 at 18:46
  • Yes, but, you are either (still) hiding some piece of code or you have set `CheckForIllegalCrossThreadCalls = false;` and that's on you. See: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103) – Jimi Apr 04 '21 at 18:47
  • nope. I haven't set `CheckForIllegalCrossThreadCalls = false;` – Smoking Diesel GR Apr 04 '21 at 18:49
  • Then `OutputHandler` throws because you're accessing UI elements from a Thread other than the UI Thread. Those events are raised in ThreadPool Threads and you're not setting the `Process.SynchronizingObject` to a Form instance. – Jimi Apr 04 '21 at 18:50

0 Answers0