什么是错的时候'不能与打开一个用户映射部分文件执行请求的操作? [英] What's wrong when 'the requested operation cannot be performed on a file with a user-mapped section open?'

查看:1331
本文介绍了什么是错的时候'不能与打开一个用户映射部分文件执行请求的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发即读应用程序,并写了很多(但同步,虽然)到磁盘上的某个文件。但是,该文件是如何大得,更多的时候我得到以下IOException异常:

I'm developing an application that is reading and writing a lot (but synchronously, though) to a certain file on disk. However, how larger the file gets, the more often I get the following IOException:

请求的操作不能被
进行上与用户映射
节打开一个文件

The requested operation cannot be performed on a file with a user-mapped section open.

这是存在的:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at 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)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
   at System.IO.File.WriteAllText(String path, String contents, Encoding encoding)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at TestApp.Program.Main()

正如你所看到的,异常是从 File.WriteAllText 中抛出。我怀疑除了曾与同时阅读和写作做的,所以试图限制对文件的访问。既是锁定互斥似乎增加异常的数目,而不是阻止他们。那么,究竟是怎么回事?这是什么例外恰恰意味着以及如何预防?

As you can see, the exception is thrown from within File.WriteAllText. I suspected the exception had to do with simultaneous reading and writing, so tried to limit access to the file. Both a lock and a Mutex seemed to increase the number of exceptions instead of preventing them. So what's exactly going on? What does this exception exactly mean and how can it be prevented?

推荐答案

我在写一个文件(打开,循环(写入),接近),但一个错误后,复制了调用rutine ...所以我结束了不能与打开一个用户映射部分文件执行请求的操作。!
哦,我删除了第二个电话,然后问题就解决了。
似乎有两个(开/关)(开/关)rutines在同一个文件发生得太快了一个又一个原因,这个...
一些开发商提出来调用GC。
检查正确关闭每个I / O操作。
不执行完整的(开放环(写入),接近)太快。看来,当一个操作它会而第二个请求到达,并抛出问题得到成品。

I was writing to a file (open,loop(writes),close) but after a mistake a duplicated the call to the rutine... so I end up with "The requested operation cannot be performed on a file with a user-mapped section open."! Well, I deleted the second call and then the problem solved. It seems that two (open/close) (open/close) rutines over the same file happening too soon one after another causes this... Some developers suggest to call the gc. Check for closing correctly every i/o operation. Not to perform complete (open,loop(writes),close) too soon. It seems that when one operation it is going to be finished while the second request arrives and that throws the problem.

这篇关于什么是错的时候'不能与打开一个用户映射部分文件执行请求的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆