I have an application capturing (using wireshark) packets and i want the option to show on my form the number of received packets ongoing.
Code:
public class Tshark
{
public int _interfaceNumber;
public string _pcapPath;
public int _packetsCount;
public string _packet;
public delegate void dlgPackProgress(int progress);
public event dlgPackProgress evePacketProgress;
public Tshark(int interfaceNumber, string pcapPath)
{
_interfaceNumber = interfaceNumber;
_pcapPath = pcapPath;
}
public void startTheCapture()
{
Process _tsharkProcess1 = new Process();
_tsharkProcess1.StartInfo.FileName = @"C:\Program Files\Wireshark\tshark.exe";
_tsharkProcess1.StartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -V -x -w " + _pcapPath);
//_tsharkProcess1.StartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -c " + int.MaxValue + " -w " + _pcapPath);
_tsharkProcess1.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
_tsharkProcess1.StartInfo.RedirectStandardOutput = true;
_tsharkProcess1.StartInfo.UseShellExecute = false;
_tsharkProcess1.StartInfo.CreateNoWindow = true;
_tsharkProcess1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_tsharkProcess1.Start();
StreamReader myStreamReader = _tsharkProcess1.StandardOutput;
while (!myStreamReader.EndOfStream)
{
_packet = myStreamReader.ReadLine();
//if (_packet.StartsWith("Frame Number: "))
//{
// string[] arr = _packet.Split(' ');
// _test = int.Parse(arr[2]);
// _packetsCount++;
//}
OnPacketProgress(_packetsCount++);
}
_tsharkProcess1.WaitForExit();
}
private void OnPacketProgress(int packet)
{
var handler = evePacketProgress;
if (handler != null)
{
handler(packet);
}
}
public void killProcess()
{
foreach (Process prc in System.Diagnostics.Process.GetProcessesByName("tshark"))
{
prc.Kill();
prc.WaitForExit();
}
}
private void process_OutputDataReceived(object sender, DataReceivedEventArgs arg)
{
string srt = arg.Data; //arg.Data contains the output data from the process...
}
}
This class start Tshark and start capturing from the main form i have this ProgressChanged that sample the class properties in order to show on my form the packets number, the problem is that if i am for example have high speen rate on my network (BitTorrent download) my form stuck, maybe i can solve this by sample the class properties every few seconds ?
private void bgWSniffer_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
tshark = e.UserState as Tshark;
lbl.Text = tshark._packetsCount.ToString();
lbl.Text = tshark._packet;
}
I know that the problematic line is OnPacketProgress(_packetsCount++);
my start button who start the capture:
private void btnStartCapture_Click(object sender, EventArgs e)
{
lblStatusSnifferTab2.Text = "Listening on ip address " + lblIpAddressSnifferTab2.Text;
timerSniffer.Start();
btnStartTabSniffer.Enabled = false;
btnStopTabSniffer.Enabled = true;
groupBoxSelectTabSniffer.Enabled = false;
groupBoxOptionsSnifferTab.Enabled = false;
groupBoxCaptureFilesTabSniffer.Enabled = false;
groupBoxStopCaptureSnifferTab.Enabled = false;
bgWorker = new BackgroundWorker();
bgWorker.WorkerReportsProgress = true;
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWSniffer_ProgressChanged);
bgWorker.DoWork += new DoWorkEventHandler(
(s3, e3) =>
{
tshark = new Tshark(2, pcapFilePathSniffer);
tshark.evePacketProgress += new Tshark.dlgPackProgress(
(packet) =>
{
bgWorker.ReportProgress(packet, tshark);
});
tshark.startTheCapture();
});
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s3, e3) =>
{
groupBoxSelectTabSniffer.Enabled = true;
groupBoxCaptureFilesTabSniffer.Enabled = true;
});
bgWorker.RunWorkerAsync();
}
One solution would be to do (used milliseconds so you can have fine control, if all you want is seconds you can use TotalSeconds):
Also, something else I noticed, you use post increment when you call OnPacketProgress. This will pass the current value of _packetsCount and then increment it after OnPacketProgress returns. You should double check whether that’s what you want or not.