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 8640675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:20:15+00:00 2026-06-12T11:20:15+00:00

i have application how open Tshark process and start capturing packet, this process create

  • 0

i have application how open Tshark process and start capturing packet, this process create pcap file on the disk and from my main form i am checking this class properties and update my GUI.

recently i have added the option that checking the file size on the disk and this property growing ongoing, my problem is that after i start my function the property that represent the file size try to check this file but if the process did not create the file yet this property is null and my application crash so i add Thread.Sleep and now it’s working but i am wonder if there is batter way to do it.

this is my class with the function who start capturing, the property that i am talking about is _myFile and the Thread.Sleep that i wand to change is after my tshark.Start();

    public class Tshark2
    {
        #region class members
        public string _tshark;
        public string _filePath;
        public List<string> _list;
        public ProcessStartInfo _process;
        public myObject _obj;
        public int _interfaceNumber;
        public string _pcapPath;
        public string _status;
        public int _receivesPackets;
        public int _packetsCount;
        public string _packet;
        public double _bitsPerSecond;
        public double _packetsPerSecond;
        public decimal _packetLimitSize;
        public DateTime _lastTimestamp;
        PacketDevice _device;
        public delegate void dlgPackProgress(int progress);
        public event dlgPackProgress evePacketProgress;
        public DirectoryInfo _directoryInfo;
        public FileInfo _myFile;
        public FileInfo _fileInfo;
        public FileInfo[] _dirs;
        public long _fileSize;

        public void startCapturing()
        {
            _status = "Listening...";
            ThreadStart tStarter = delegate { openAdapterForStatistics(_device); };
            Thread thread = new Thread(tStarter);
            thread.IsBackground = true;
            thread.Start();

            Process tshark = new Process();
            tshark.StartInfo.FileName = _tshark;
            tshark.StartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -V -x -s " + _packetLimitSize + " -w " + _pcapPath);
            tshark.StartInfo.RedirectStandardOutput = true;
            tshark.StartInfo.UseShellExecute = false;
            tshark.StartInfo.CreateNoWindow = true;
            tshark.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            tshark.Start();
            Thread.Sleep(1000);
            DateTime lastUpdate = DateTime.MinValue;
            StreamReader myStreamReader = tshark.StandardOutput;
            _fileInfo = new FileInfo(_pcapPath);
            string directoryName = _fileInfo.DirectoryName;
            _directoryInfo = new DirectoryInfo(directoryName);
            _dirs = _directoryInfo.GetFiles();
            _myFile = _dirs.FirstOrDefault(f => f.Name.Equals(_fileInfo.Name));

            while (!myStreamReader.EndOfStream)
            {
                _packet = myStreamReader.ReadLine();

                if (_packet.StartsWith("    Frame Number:"))
                {
                    string[] arr = _packet.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
                    _receivesPackets = int.Parse(arr[2]);
                    _packetsCount++;
                }

                if ((DateTime.Now - lastUpdate).TotalMilliseconds > 1000)
                {
                    lastUpdate = DateTime.Now;
                    OnPacketProgress(_packetsCount++);
                }
            }

            tshark.WaitForExit();
        }
}
  • 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-12T11:20:16+00:00Added an answer on June 12, 2026 at 11:20 am

    Try Process.WaitForExit instead of a fixed sleep time. See http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx.

    Edit:
    WaitForExit should go prior to reading the output of the process you are spawning, if you are attempting to utilize the output of the process. In that scenario, WaitForExit would replace Thread.Sleep. If you want to monitor the filesize as it is being written, wait for the file to be created and then use a timer to check the progress of the file as written. See the example below.

    for (int i = 20 /* seconds till exception */; i > 0; i--)
    {
        if (File.Exists(_pcapPath))
            break;
        else if (i == 1)
            throw new Exception("File was not created within 20 seconds.");
        else
            Thread.Sleep(1000);
    }
    
    while (!tshark.WaitForExit(1000 /* file size update interval */))
    {
        var fileInfo = new FileInfo(_pcapPath);
        Console.WriteLine("File has grown to {0} bytes", fileInfo.Length);
    }
    
    Console.WriteLine("Complete");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my application i m open Wireshark process and start capturing packts, in the
I have application for iphone i want to open it from start up page
I have built an open-source application from the source code. Unfortunately, the original executable
I have window form in my WPF Application when I open new window from
I have an application that generates Open XML documents with Content Controls. To create
I have an application that would create a file of some extension, let say
I need from my C# application open a PDF file by using Foxit Reader
I have a 64 bit Enterprice SuSE 11 I have an application which open
When no users have the any pages in an application open, it will unload
We have an application using the IBM Informix driver. Whenever we try to open

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.