Redirect Console Output to Textbox in Separate Program

Redirect console output to textbox in separate program

This works for me:

void RunWithRedirect(string cmdPath)
{
var proc = new Process();
proc.StartInfo.FileName = cmdPath;

// set up output redirection
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.CreateNoWindow = true;
// see below for output handler
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;

proc.Start();

proc.BeginErrorReadLine();
proc.BeginOutputReadLine();

proc.WaitForExit();
}

void proc_DataReceived(object sender, DataReceivedEventArgs e)
{
// output will be in string e.Data
}

What is a good way to direct console output to Text-box in Windows Form?

Create a text writer which writes to a text box:

    public class TextBoxWriter : TextWriter
{
TextBox _output = null;

public TextBoxWriter (TextBox output)
{
_output = output;
}

public override void Write(char value)
{
base.Write(value);
_output.AppendText(value.ToString());
}

public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}

And redirect Console output to this writer:

        //...

public Form()
{
InitializeComponent();
}

private void Form_Load(object sender, EventArgs e)
{
Console.SetOut(new TextBoxWriter(txtConsole));
Console.WriteLine("Now redirecting output to the text box");
}

How to redirect Output of console application to Textbox control on a windows form at run time using vb.net 2012

Looks like you are trying to make a call from a thread that is different from the thread the form is on. The events raised from the Process class will not be from the same thread.

Delegate Sub UpdateTextBoxDelg(text As String)
Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)

Public Sub UpdateTextBox(text As String)
Textbox.Text = text
End Sub

Public Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)

If Me.InvokeRequired = True Then
Me.Invoke(myDelegate, e.Data)
Else
UpdateTextBox(e.Data)
End If

End Sub

Redirecting command prompt process output to WPF TextBox

I have an off-beat suggestion, but I have used it for something similar.

In the System.Diagnostics namespace, there is a class called Trace.

Trace is for diagnostics, but its great at message relay.

you write messages to the buffer like this:

System.Diagnostics.Trace.TraceInformation($"Something happened at {DateTime.Now}");

to subscribe or "listen" to the stream of messages, you can:

using System.Diagnostics;
// ## IMPORTANT - set 'AutoFlush' or you will have to call Flush() manually at some interval
Trace.AutoFlush = true;
// log to console.
Trace.Listeners.Add(new ConsoleWriterTraceListener(true));
// log to file.
Trace.Listeners.Add(new TextWriterTraceListener(@"c:\temp\applog.txt"));
// write something custom by inheriting from the base class.
Trace.Listeners.Add(new YourCustomTraceListener());

The beauty of this is the async message relay stuff is all handled by the framework, you just need to send and receive messages.

NOTE: You can also manage the set of listeners in the config file:

<system.diagnostics>
<trace>
<listeners autoflush="true" >
<add type="System.Diagnostics.TextWriteTraceListener" initialData="c:\temp\applog.txt" name="default" />
</listeners>
</trace>
</system.diagnostics>

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


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.



Related Topics



Leave a reply



Submit