路过秋天

同样的3年,有的人从学生到当了MVP了,而我却在原地,卖弄当年的代码,秋天的风,有点凄,有点凉!

公告信息
内涵是很强大的~~~别看外表~~~当犀利哥入侵不了的时候,感觉有种莫名的失落~~~
文章档案
最新评论

C# 单线程也锁不住的IO写操作异常(神奇BUG)

最近看日志,发现记录了这样一个日志:

log:http://cyqdata.com/search/cnblogs/finger+print
------------------------
Error On : 2013-3-28 5:30:18
[WriteException]:文件“D:\*\TableSchema\CYQ.Data.TableSchema_Sql.qblog.Blog_User.ts”正由另一进程使用,因此该进程无法访问该文件。:
   在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   在 System.IO.StreamWriter.CreateFile(String path, Boolean append)
   在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   在 System.IO.StreamWriter..ctor(String path, Boolean append)
   在 CYQ.Data.Tool.IOHelper.Save(String fileName, String text, Boolean isAppend)
   在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   在 System.IO.StreamWriter.CreateFile(String path, Boolean append)
   在 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   在 System.IO.StreamWriter..ctor(String path, Boolean append)
   在 CYQ.Data.Tool.IOHelper.Save(String fileName, String text, Boolean isAppend)

根据错误,推想是应用程序池在回收时,产生的另一个进程,刚好写文件时和另一个未回收完成的进程同时写引发的异常,被记录了。

所以我就想了一个招,打算增加多进程间并发解决方案,当然就是用的:

 static System.Threading.Semaphore _mutex = new System.Threading.Semaphore(1, 1, "IOHelper.Save");

通过进程间的互斥量处理。

写完代码,我弄了个项目,写个了Demo,运行。。。悲催的事情发生了,无耻的BUG。

运行了一下,我查看文件是否正常和成,于是我打开那文件,看日志写进了没有,发现很正常写进了,于是又回到代码处。

然后开了两个进程运行。。一个BUG就这样发生了:

"文件“F:\\*\\bin\\Debug\\a.ts”正由另一进程使用,因此该进程无法访问该文件。"

之后,不管我怎么写代码,单线程,Lock,双重Lock,还是Mutex,都抛这个异常。

然后我加了线程休眠,发现加到Thread.Slee(100)以下又正常。

让我怀疑,难道文件关闭,还有延时功能。。。

之后前前左左折腾了几小时。。。你妹夫。。后来不弄了。

我把文件夹之类的都关了,过了点时间,我又回去弄了下,突然运行很正常了。

经过几个轮回折腾,发现以下神奇的BUG:

原来是操作的文件,被鼠标选定了原因(之前打开过文件,所以该文件里一直是选定状态)。

所以,大伙懂了,文件被选定,BUG就出来了,取消文件选定,一切正常了。

秋色园是QBlog的官方站点,由路过秋天创建,基于cyqdata数据层框架开发的支持多用户、多语言、多数据库(access,mssql,oracle)、目录级url等功能强大的博客系统
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"

2013/3/29 0:36:11 | 记录点滴 | |

  • 发表评论