难道FileStream.Dispose立即关闭该文件? [英] Does FileStream.Dispose close the file immediately?
问题描述
我有一些code,通过使用MemoryStream.WriteTo保存的MemoryStream到一个FileStream写入一个文件()。该文件后关闭它被再次打开,阅读一些metdata ...
I have some code that writes a file by saving a MemoryStream to a FileStream using MemoryStream.WriteTo(). After the file is closed it is opened up again to read some metdata...
本作品约80 - 90%的时间。其他的20%我得到一个异常说,该文件是由另一个进程使用。
This works about 80 - 90% of the time. The other 20% I get an exception saying the file is "in use by another process".
难道FileStream.Dispose()不同步释放资源?是不是有什么打算降低对Win32的土地,我不知道呢?我没有看到任何明显的净文档。
Does FileStream.Dispose() not release resources synchronously? Is there something going on lower in Win32 land I'm not aware of? I'm not seeing anything obvious in the .Net documentation.
推荐答案
由于立即成为可能。有可以方便地将一定的滞后性,由于<打击>优秀写入罢>,延迟更新目录信息等,也可能是反病毒软件检查您更改的文件。
As "immediately" as possible. There can easily be some lag due to outstanding writes, delay in updating the directory info etc. It could also be anti-virus software checking your changed file.
这可能是一个罕见的情况下,一个的Thread.Sleep(1)
被调用。但是是完全安全的,你将不得不赶上(任何)异常,然后再试一次次的一组数字。
This may be a rare case where a Thread.Sleep(1)
is called for. But to be totally safe you will have to catch the (any) exception and try again a set number of times.
这篇关于难道FileStream.Dispose立即关闭该文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!