title: Creating Then Deleting File Causes IOException
body: <p>An application needs to create a file in a directory, do something in the directory, and then delete the file. For example, the source code below:</p>
  
  <pre><code>File.Create("textfile.txt");
  // Do something here
  File.Delete("textfile.txt");
  </code></pre>
  
  <p>If "something" is a process that only needs a very short amount of time, File.Delete will throw IOException (file is being used by another process). According to another SO post: <a href="https://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true">Cannot delete directory with Directory.Delete(path, true)</a>, calling Thread.Sleep(0) should allow the previous process to finish. However, even with</p>
  
  <pre><code>File.Create("textfile.txt");
  // Do something here
  Thread.Sleep(0);
  File.Delete("textfile.txt");
  </code></pre>
  
  <p>the same IOException is still be thrown.</p>
  
  <p>The solution I got is a while-loop that try to delete the file repeatedly until it's deleted. But I'm wondering if theres' a better solution.</p>