How to wait until my process will create my file on the disk to avoid null when i am checking this file
<p>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.</p>

<p>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.</p>

<p>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();</p>

<pre><code>    public class Tshark2
    {
        #region class members
        public string _tshark;
        public string _filePath;
        public List&lt;string&gt; _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 =&gt; 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 &gt; 1000)
                {
                    lastUpdate = DateTime.Now;
                    OnPacketProgress(_packetsCount++);
                }
            }

            tshark.WaitForExit();
        }
}
</code></pre>
