How do I redirect a console program's output to a text box in a thread safe way?

前端 未结 2 2092
情歌与酒
情歌与酒 2021-02-03 15:13

I am having trouble redirecting console output to a windows forms text box. The problem is thread related. I am running a console app in the following way,

priva         


        
相关标签:
2条回答
  • 2021-02-03 15:15

    try

    out_txtbx.Invoke(new AppendTextDelegate(this.AppendText), text);
    
    0 讨论(0)
  • 2021-02-03 15:22
    proc.WaitForExit();
    

    It is called deadlock. Your main thread is blocked, waiting for the process to exit. That stops it from taking care of essential duties. Like keeping the UI updated. And making sure that Control.Invoke() requests are dispatched. That stops the AppendText() method from completing. Which stops the process for exiting. Which stops your UI thread from ever getting past the WaitForExit() call. "Deadly embrace", aka deadlock.

    You cannot block your main thread. Use the Process.Exited event instead.

    0 讨论(0)
提交回复
热议问题