开发者

Redirect standart stream (Process.Start)

开发者 https://www.devze.com 2023-01-20 02:50 出处:网络
I have the following code: private void button1_Click(object sender, EventArgs e) { Process process = new Process();

I have the following code:

 private void button1_Click(object sender, EventArgs e)
    {
        Process process = new Process();
        ProcessStartInfo processStartInfo = new ProcessStartInfo();

        processStartInfo.Arguments = "-i 1 -x";
        processStartInfo.CreateNoWindow = false;
        processStartInfo.FileName = @"F:\NET\WiresharkPortable\App\Wireshark\tshark.exe";
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.UseShellExecute = false;
        process.StartInfo = processStar开发者_JAVA百科tInfo;
        process.Start();

        StreamReader streamReader = process.StandardOutput;
        textBox1.AppendText(streamReader.ReadToEnd() + "\r\n");
    }

I'm trying to redirect the output to my program. tshark is sniffer, so it works until was suspended. How to redirect data in real time? Thanks.


You're currently calling ReadToEnd, which will block to the end of the stream.

You could either repeatedly call Read from a separate thread, or you could use the newer eventing approach, handling Process.OutputDataReceived for each line after calling BeginOutputReadLine. Don't forget to marshal back to the UI thread before changing the textbox.

0

精彩评论

暂无评论...
验证码 换一张
取 消