Redirect standart stream (Process.Start)
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.
精彩评论