Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8607323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:19:35+00:00 2026-06-12T03:19:35+00:00

I have an application capturing (using wireshark) packets and i want the option to

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:19:36+00:00Added an answer on June 12, 2026 at 3:19 am

    One solution would be to do (used milliseconds so you can have fine control, if all you want is seconds you can use TotalSeconds):

        DateTime lastUpdate = DateTime.MinValue;
    
        while (!myStreamReader.EndOfStream)
        {
            _packet = myStreamReader.ReadLine();
    
            //if (_packet.StartsWith("Frame Number: "))
            //{
            //    string[] arr = _packet.Split(' ');
            //    _test = int.Parse(arr[2]);
            //    _packetsCount++;
            //}
    
            // If more than one second
            if((DateTime.Now - lastUpdate).TotalMilliseconds > 1000)
            {
                lastUpdate = DateTime.Now;
                OnPacketProgress(_packetsCount++);
            }
        }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# WinForm application where I am using a ListView to show
i have application how open Tshark process and start capturing packet, this process create
I have an application with a static class that is capturing all errors that
in my application i m open Wireshark process and start capturing packts, in the
I'm having trouble capturing the <tab> keystroke in my Java command-line application. Using System.in.read()
I am using tesseract library in my application for the image scanning. I have
I have a Windows Mobile application using the compact framework (NETCF) that I would
I have application which uses Sherlock ActionBar package. The application uses platform-specific behavior for
I have application where i have two view controllers my first view and second
I have application with needs to have access to some sensitive data(in this case

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.