1

Tshark プロセスを開いてパケットのキャプチャを開始するアプリケーションがあります。このプロセスはディスク上に pcap ファイルを作成し、メイン フォームからこのクラス プロパティをチェックして GUI を更新します。

最近、ディスク上のファイル サイズをチェックするオプションを追加しました。このプロパティは進行中です。私の問題は、関数を開始した後、ファイル サイズを表すプロパティがこのファイルをチェックしようとするが、プロセスがファイルを作成しなかった場合です。それでも、このプロパティは null で、アプリケーションがクラッシュするため、Thread.Sleep を追加すると機能するようになりましたが、それを行うバッターの方法があるかどうか疑問に思っています。

これはキャプチャを開始する関数を持つ私のクラスです。私が話しているプロパティは _myFile であり、変更したい Thread.Sleep は私の 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();
        }
}
4

1 に答える 1

0

固定のスリープ時間の代わりに Process.WaitForExit を試してください。http://msdn.microsoft.com/en-us/library/ty0d8k56.aspxを参照してください。

編集: プロセスの出力を利用しようとしている場合は、生成しているプロセスの出力を読み取る前に WaitForExit を実行する必要があります。そのシナリオでは、WaitForExit が Thread.Sleep を置き換えます。書き込み中のファイルサイズを監視する場合は、ファイルが作成されるのを待ってから、タイマーを使用してファイルの書き込みの進行状況を確認します。以下の例を参照してください。

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");
于 2012-10-06T20:50:52.057 に答える