3

I have been trying to grab the console output from the following to

private void List_Adapter()
    {
        using (Process tshark = new Process())
        {
            tshark.StartInfo.FileName = ConfigurationManager.AppSettings["fileLocation"];
            tshark.StartInfo.Arguments = "-D";
            tshark.StartInfo.CreateNoWindow = true;
            tshark.StartInfo.UseShellExecute = false;
            tshark.StartInfo.RedirectStandardOutput = true;

           tshark.OutputDataReceived += new DataReceivedEventHandler(TSharkOutputHandler);

            tshark.Start();

            tshark.BeginOutputReadLine();
            tshark.WaitForExit();
        }
    }

    void TSharkOutputHandler(object sender, DataReceivedEventArgs e)
    {
        this.Dispatcher.Invoke((Action)(() =>
        {
            tboxConsoleOutput.AppendText(e.Data);
        }));
    } 

But the ui just freezes, no info is being displayed am I just approaching this incorrectly

I have found the following and tried with no luck

No access different thread
Object different thread
Redirect Output to Textbox
Output to Textbox
Process output to richtextbox

Community
  • 1
  • 1
ondrovic
  • 1,105
  • 2
  • 23
  • 40
  • Your UI freezes because of this statement: `tshark.WaitForExit();` You are blocking the UI thread, while the process runs. In fact, depending on how much output the process makes, you may be eventually preventing the process from continuing, because you're deadlocked on the `Invoke()` method call. It's not possible to provide a good answer, because there's not enough context in your question. But it will involve not calling `WaitForExit()` (and not disposing the `Process` object until you're done with it). – Peter Duniho Mar 27 '17 at 01:09

1 Answers1

4

Here is how I am doing that:

First you implement the following class:

public class TextBoxConsole : TextWriter
{
    TextBox output = null; //Textbox used to show Console's output.

    /// <summary>
    /// Custom TextBox-Class used to print the Console output.
    /// </summary>
    /// <param name="_output">Textbox used to show Console's output.</param>
    public TextBoxConsole(TextBox _output)
    {
        output = _output;
        output.ScrollBars = ScrollBars.Both;
        output.WordWrap = true;
    }

    //<summary>
    //Appends text to the textbox and to the logfile
    //</summary>
    //<param name="value">Input-string which is appended to the textbox.</param>
    public override void Write(char value)
    {
        base.Write(value);
        output.AppendText(value.ToString());//Append char to the textbox
    }


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

Now if you want all your Console Output to be written to a certain textbox you declare it as follows.

First create a textbox and name i.e. "tbConsole". Now you want to tell it what to do:

TextWriter writer = new TextBoxConsole(tbConsole);
Console.SetOut(writer);

From now on every time you write something like Console.WriteLine("Foo"); it will be written to your textbox. That's it. Note this approach is not mine. Also depending on how much output your Console produces it might have poor performance because it writes the output char by char.

チーズパン
  • 2,752
  • 8
  • 42
  • 63