title: How to wait until File.Exists?
body: <p>I have an app, listening for the *.log file in a chosen folder. I used <code>FileSystemWatcher</code>.</p>
  
  <p>But there is a problem. The other app responsible for making that file takes following steps:</p>
  
  <ol>
  <li>Make a *.gz file</li>
  <li>Unpack it to txt file (some random file name)</li>
  <li>Change the *.txt name to proper one with *.log extension. </li>
  </ol>
  
  <p>And I can not change this behaviour. </p>
  
  <p>So I made 2 <code>FileSystemWatcher</code>s for *.gz, and *.txt files. Why? Because this app sometimes doesn't unpack the gz file, and sometimes doesn't rename the txt file to the final *.log file. </p>
  
  <p><code>FileSystemWatcher2</code> catches txt file, then (in the most cases it is renamed to log in the next 1000ms) I need to wait some time to check if txt file exists (if not, it seems to be renamed to the final *.log file). </p>
  
  <p>The question is, how to check if file exists without <code>Thread.Sleep()</code> to prevent UI freeze?  </p>
  
  <p>I hope it is clear, if not I will try to describe it better. I think this is a complex problem.</p>
  
  <p>Some code sample:</p>
  
  <p>Watcher for gz file:</p>
  
  <pre><code>private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
  {
     //this is for gz files in case if gz file is not unpacked automatically by other app
     //I need to wait and check if gz was unpacked, if not, unpack it by myself,
     //then txt watcher will catch that
     Thread.Sleep(5000);
     if (File.Exists(e.FullPath))
     {
        try
        {
           byte[] dataBuffer = new byte[4096];
           using (System.IO.Stream fs = new FileStream(e.FullPath, 
                                                       FileMode.Open, 
                                                       FileAccess.Read))
           {
              using (GZipInputStream gzipStream = new GZipInputStream(fs))
              {                            
                 string fnOut = Path.Combine(path_to_watcher, 
                                             Path.GetFileNameWithoutExtension(e.FullPath));
  
                 using (FileStream fsOut = File.Create(fnOut))
                 {
                    StreamUtils.Copy(gzipStream, fsOut, dataBuffer);
                 }                            
              }
           }
        }
        catch { //Ignore }
     }
  }
  </code></pre>
  
  <p>Watcher for txt file:</p>
  
  <pre><code>private void fileSystemWatcher2_Created(object sender, FileSystemEventArgs e)
  {
     //this is for txt file
     Thread.Sleep(3500);
     if (File.Exists(e.FullPath))
     {
        //make my actions
     }
     else
     {
        //make my actions
     }
  }
  </code></pre>