0

in my application i am open Tshark process and start capturing and from my main form i am update my UI. one of my class properties is my file how created (pcap file) and on my UI i am update this file length ongoing. my problem is that if this file is new the length is remain zero but if my file is file that already exist my UI show my the length ongoing (of course the file is overwritten but i can see it growing).

    public class Tshark
    {
        #region members
        private Process _tsharkProcess = new Process();
        private int _numberOfPackets;
        private string _tshark;
        private List<string> _list;
        private ProcessStartInfo _process;
        private int _interfaceNumber;
        private string _pcapPath;
        private string _status;
        private double _bitsPerSecond;
        private double _packetsPerSecond;
        private decimal _packetLimitSize;
        private DateTime _lastTimestamp;
        private PacketDevice _device;
        private FileInfo _file;
        private bool _processRunning;

        public void startCapturing()
        {            
            ThreadStart tStarter = delegate { openAdapterForStatistics(_device); };
            Thread thread = new Thread(tStarter);
            thread.IsBackground = true;
            thread.Start();
            ProcessStartInfo tsharkStartInfo = new ProcessStartInfo();
            tsharkStartInfo.FileName = _tshark; 
            tsharkStartInfo.RedirectStandardOutput = true; 
            tsharkStartInfo.RedirectStandardError = true; 
            tsharkStartInfo.RedirectStandardInput = true; 
            tsharkStartInfo.UseShellExecute = false; 
            tsharkStartInfo.CreateNoWindow = true; 
            tsharkStartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -s " + _packetLimitSize + " -w " + _pcapPath);
            _tsharkProcess.StartInfo = tsharkStartInfo;
            _tsharkProcess.ErrorDataReceived += _cmdProcess_ErrorDataReceived;
            _tsharkProcess.OutputDataReceived += tshark_OutputDataReceived; 
            _tsharkProcess.EnableRaisingEvents = true; 
            _tsharkProcess.Start();
            _processRunning = true;
            _tsharkProcess.BeginOutputReadLine(); 
            _tsharkProcess.BeginErrorReadLine();
_tsharkProcess.WaitForExit(); 
        }

    public long getFileLength()
    {
        _file = new FileInfo(_pcapPath);
        string directoryName = _file.DirectoryName;
        DirectoryInfo directoryInfo = new DirectoryInfo(directoryName);
        FileInfo[] dirs = directoryInfo.GetFiles();
        _file = dirs.FirstOrDefault(f => f.Name.Equals(_file.Name));

        if (_file != null)
        {

            return _file.Length;
        }

        return 0;
    }

        public void refreshFile()
        {            
            if (_file != null)
            {
                _file.Refresh();
            }
        }
}

from the main form i am update my UI via timer:

private void tmrUpdateLbl_Tick(object sender, EventArgs e)
{
    lblFileSizeTabSniffer2.Text = formatBytes(tshark.getFileLength());
    tshark.refreshFile();
}

in the dibugger i can see that if the file is new one _file is null all the time so in getFileLength() method return zero.

4

1 に答える 1

0

You need to get the full FileInfo every time (i.e. in getFileLength) to get up-to-date results. Move relevant code from startCapturing to getFileLength.

于 2012-10-18T17:48:39.283 に答える