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

  • Home
  • SEARCH
  • 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 8591315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:29:54+00:00 2026-06-11T23:29:54+00:00

Here is my method that starts a process: public class Tshark { public int

  • 0

Here is my method that starts a process:

public class Tshark
{
    public int _interfaceNumber;
    public string _pcapPath;
    Process _tsharkProcess;
    public int _packetsCount;
    public string _packet;
    public string _tsharkPath = @"C:\Program Files\Wireshark\tshark.exe";
    public delegate void dlgPackProgress(int progress);
    public event dlgPackProgress evePacketProgress;

    public Tshark(int interfaceNumber, string pcapPath)
    {
        _interfaceNumber = interfaceNumber;
        _pcapPath = pcapPath;
    }

    public void startTheCapture()
    {
        try
        {
            _tsharkProcess = new Process();
            _tsharkProcess.StartInfo.FileName = @"C:\Program Files\Wireshark\tshark.exe";
            _tsharkProcess.StartInfo.Arguments = string.Format("-i " + _interfaceNumber);
            _tsharkProcess.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
            _tsharkProcess.StartInfo.RedirectStandardOutput = true;
            _tsharkProcess.StartInfo.UseShellExecute = false;
            _tsharkProcess.StartInfo.CreateNoWindow = true;
            _tsharkProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            _tsharkProcess.Start();
            StreamReader myStreamReader = _tsharkProcess.StandardOutput;

            while (!myStreamReader.EndOfStream)
            {
                _packet = myStreamReader.ReadLine();
                OnPacketProgress(_packetsCount++);
            }

            _tsharkProcess.WaitForExit();
        }
        catch (Exception e)
        {

        }
    }

    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...            
    }
}

The method startTheCapture() is invoked from the main form :

private void btnStartCapture_Click(object sender, EventArgs e)
{
    BackgroundWorker bgWorker = new BackgroundWorker();
    bgWorker.WorkerReportsProgress = true;
    bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWSniffer_ProgressChanged);
    bgWorker.DoWork += new DoWorkEventHandler(
        (s3, e3) =>
        {
            tshark = new Tshark(2, pcapFilePathSniffer);
            tshark.startTheCapture();

            tshark.evePacketProgress += new Tshark.dlgPackProgress(
                (packet) =>
                {
                    bgWorker.ReportProgress(packet, tshark);
                });
        });

    bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s3, e3) =>
        {

        });

    bgWorker.RunWorkerAsync();

}

Now this is from the main form too, I have written ProgressChanged which is supposed to update my gui but my problem is that this is not working and I cannot figure why.

private void bgWSniffer_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    tshark = e.UserState as Tshark;
    listBoxPacketsSnifferTab.Text += tshark._packet;
    lblPacketsReceived2.Text = tshark._packetsCount.ToString("#,##0");
}
  • 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-11T23:29:56+00:00Added an answer on June 11, 2026 at 11:29 pm

    in your DoWorkEventHandler, you call tshark.StartTheCapture() before you hookup evePacketProgress event handler.

    The startTheCapture method blocks until the wireshark process exits, so any progress events captured while it’s running will be “ignored” in OnPacketProgress because there is no event subscriber (handler == null).

    change it to this:

    bgWorker.DoWork += new DoWorkEventHandler(
        (s3, e3) =>
        {
            tshark = new Tshark(2, pcapFilePathSniffer);
    
            //hookup event handler before executing method
            tshark.evePacketProgress += new Tshark.dlgPackProgress(
                (packet) =>
                {
                    bgWorker.ReportProgress(packet, tshark);
                });
    
            tshark.startTheCapture();
    
    
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's the method: public static int chooseStrat () { String[] strats = new String[1]
I call addNotify() method in class that I posted here. The problem is, that
Here is the question: write a method that swaps two variables. These two variables
Just a quick newbie question here. I have a method that calculates a value
The method in question that I want to use is gem and sourced here
I have two classes: Action class, that has a method for executing VBScript files,
I'm trying to create a class in java that pool objects. The class starts
public class MainClass { private static final int size = 5; private ExecutorService prodExec
public class MainClass { private static final int producerPoolSize = 10; private static final
I've got a few controllers here at work that contain methods I can use

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.