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:
File.Create("textfile.txt");
// Do something here
File.Delete("textfile.txt");
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: Cannot delete directory with Directory.Delete(path, true), calling Thread.Sleep(0) should allow the previous process to finish. However, even with
File.Create("textfile.txt");
// Do something here
Thread.Sleep(0);
File.Delete("textfile.txt");
the same IOException is still be thrown.
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.