title: C# Wait for pending IO operations
body: <p>Given a directory, how can I wait until it has no pending IO operations (with an optional timeout).</p>
  
  <ul>
  <li>I might be copying, moving or deleting many files.</li>
  <li>If one operation fails, they all need to be reversed.</li>
  <li>Some operations are dependent on others (E.G When deleting a file,
  create the backup directory, move the file)</li>
  </ul>
  
  <p>E.G</p>
  
  <pre><code>string targetParentDirectory = "C:\\mydir";
  int timeOut = 500;
  
  //Do many IO operations in targetParentDirectory
  
  WaitForPendingOperations(targetParentDirectory, timeOut);
  
  //Continue main thread
  </code></pre>
  
  <p>I can use the one FileSystemWatcher per operation I guess.</p>
  
  <ul>
  <li>Watching for each individual operation is very slow. </li>
  <li>Watching for all operations at the same time is problematic when
  having a timeout (starting every thread, making sure the thread is
  active, having a large enough timeout to wait for all of this plus the actual file operation...)</li>
  </ul>
  
  <p>Is there a better way?</p>