【发布时间】:2012-02-05 06:40:22
【问题描述】:
我正在开发一个多线程应用程序。我的代码中有某处:
File.Delete(sidetapedata);
File.Move(sidetapedata2, sidetapedata); //sidetapedata and sidetapedata2 are two file paths that correspond to sidetapedata.txt and sidetaptdata2.txt in some directory.
第二行有时运行良好,有时会抛出 IOException :
Cannot create a file when that file already exists.
还有一个线程正在访问sidetapedata 文件,但该线程仅读取此文件,没有写入操作。我正在使用锁来保护竞争条件。不知道为什么会这样。
更新:即使 Visual c# 调试器向我显示此异常,查看包含这些文件的目录,我看到没有 sidetapedata.txt 文件但有sidetapedata2.txt 文件!
UPDATE2:此外,这种行为仅在 sidetapedata.txt 和 sidetapedata2.txt 均为空白时发生
【问题讨论】:
-
在删除文件之前重命名文件,Windows/antivirus/etc 有时会在您要求 Windows 删除文件后片刻使文件“存在”。
标签: c# multithreading race-condition ioexception