I'm trying to redirect the standard
and error
outputs of my process (written in VB.NET), where i'm executing a continuous command. It is a tshark command (a command line tool of Wireshark) that captures network traffic at runtime. I tried both of the following commands:
-i 5 -B 1 -w /sample.pcap --print -Tfields -e frame.number -e ip.addr -e tcp -e _ws.col.Info -E separator=/t
-i 10 -T fields -e dns.qry.name src port 53
Both commands works great in command prompt. However when trying to redirect the output in code, only command number 1 works while the second command stuck when executing StreamReader.ReadLine
.
Please note that i know that ReadLine
waits for a new line to be read by the stream, where both above commands generates a new line of output for each captured packet. I did also try using Read
and ReadBlock
(regarding needed changes in code) but none worked for the second command.
Here is my code:
Public Class Form1
Dim output As String
Dim oProcess As New Process()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim oStartInfo As New ProcessStartInfo("C:\Program Files\Wireshark\tshark.exe", "-i 10 -T fields -e dns.qry.name src port 53")
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oStartInfo.RedirectStandardError = True
oStartInfo.CreateNoWindow = True
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
oProcess.StartInfo = oStartInfo
Catch ex As Exception
MsgBox(ex)
End Try
BackgroundWorker1.RunWorkerAsync()
Button1.Enabled = False
Button2.Enabled = True
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Threading.Thread.Sleep(2000)
If oProcess.Start() Then
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
sOutput = oStreamReader.ReadLine
While Not sOutput Is Nothing
output = sOutput & vbNewLine
BackgroundWorker1.ReportProgress(10)
sOutput = sOutput + vbNewLine + oStreamReader.ReadLine
End While
End Using
Using oStreamReader As System.IO.StreamReader = oProcess.StandardError
sOutput = oStreamReader.ReadLine
While Not sOutput Is Nothing
output = sOutput & vbNewLine
BackgroundWorker1.ReportProgress(10)
sOutput = sOutput + vbNewLine + oStreamReader.ReadLine
End While
End Using
Else
MsgBox("Error starting the process")
End If
Catch ex As Exception
MsgBox(ex)
End Try
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = output
TextBox1.Select(0, 0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BackgroundWorker1.CancelAsync()
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class