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 = processStartInfo;
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
Readfrom a separate thread, or you could use the newer eventing approach, handlingProcess.OutputDataReceivedfor each line after callingBeginOutputReadLine. Don’t forget to marshal back to the UI thread before changing the textbox.