<small id='HSTKn'></small><noframes id='HSTKn'>

    1. <legend id='HSTKn'><style id='HSTKn'><dir id='HSTKn'><q id='HSTKn'></q></dir></style></legend>
      • <bdo id='HSTKn'></bdo><ul id='HSTKn'></ul>

        <i id='HSTKn'><tr id='HSTKn'><dt id='HSTKn'><q id='HSTKn'><span id='HSTKn'><b id='HSTKn'><form id='HSTKn'><ins id='HSTKn'></ins><ul id='HSTKn'></ul><sub id='HSTKn'></sub></form><legend id='HSTKn'></legend><bdo id='HSTKn'><pre id='HSTKn'><center id='HSTKn'></center></pre></bdo></b><th id='HSTKn'></th></span></q></dt></tr></i><div id='HSTKn'><tfoot id='HSTKn'></tfoot><dl id='HSTKn'><fieldset id='HSTKn'></fieldset></dl></div>

        <tfoot id='HSTKn'></tfoot>

      1. C# 文件读/写文件共享似乎不起作用

        时间:2023-06-03

          <bdo id='Hs1Pb'></bdo><ul id='Hs1Pb'></ul>
        • <i id='Hs1Pb'><tr id='Hs1Pb'><dt id='Hs1Pb'><q id='Hs1Pb'><span id='Hs1Pb'><b id='Hs1Pb'><form id='Hs1Pb'><ins id='Hs1Pb'></ins><ul id='Hs1Pb'></ul><sub id='Hs1Pb'></sub></form><legend id='Hs1Pb'></legend><bdo id='Hs1Pb'><pre id='Hs1Pb'><center id='Hs1Pb'></center></pre></bdo></b><th id='Hs1Pb'></th></span></q></dt></tr></i><div id='Hs1Pb'><tfoot id='Hs1Pb'></tfoot><dl id='Hs1Pb'><fieldset id='Hs1Pb'></fieldset></dl></div>

          1. <legend id='Hs1Pb'><style id='Hs1Pb'><dir id='Hs1Pb'><q id='Hs1Pb'></q></dir></style></legend>
                <tbody id='Hs1Pb'></tbody>

                <small id='Hs1Pb'></small><noframes id='Hs1Pb'>

              • <tfoot id='Hs1Pb'></tfoot>
                • 本文介绍了C# 文件读/写文件共享似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我的问题是基于继承了大量我无能为力的遗留代码.基本上,我有一个可以产生数据块的设备.一个库会调用设备来创建该数据块,出于某种原因,我不完全理解并且即使我想也无法更改,它将该数据块写入磁盘.

                  My question is based off of inheriting a great deal of legacy code that I can't do very much about. Basically, I have a device that will produce a block of data. A library which will call the device to create that block of data, for some reason I don't entirely understand and cannot change even if I wanted to, writes that block of data to disk.

                  此写入不是即时的,最多可能需要 90 秒.在那个时候,用户想要获得正在生成的数据的部分视图,所以我想要一个消费者线程来读取另一个库正在写入磁盘的数据.

                  This write is not instantaneous, but can take up to 90 seconds. In that time, the user wants to get a partial view of the data that's being produced, so I want to have a consumer thread which reads the data that the other library is writing to disk.

                  在我接触这个遗留代码之前,我想用我完全控制的代码来模拟这个问题.我使用 C#,表面上是因为它提供了很多我想要的功能.

                  Before I even touch this legacy code, I want to mimic the problem using code I entirely control. I'm using C#, ostensibly because it provides a lot of the functionality I want.

                  在生产者类中,我有这段代码创建一个随机数据块:

                  In the producer class, I have this code creating a random block of data:

                  FileStream theFS = new FileStream(this.ScannerRawFileName, 
                    FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                  //note that I need to be able to read this elsewhere...
                  BinaryWriter theBinaryWriter = new BinaryWriter(theFS);
                  int y, x;
                  for (y = 0; y < imheight; y++){
                      ushort[] theData= new ushort[imwidth];
                      for(x = 0; x < imwidth;x++){
                         theData[x] = (ushort)(2*y+4*x);
                      }
                      byte[] theNewArray = new byte[imwidth * 2];
                      Buffer.BlockCopy(theImage, 0, theNewArray, 0, imwidth * 2);
                      theBinaryWriter.Write(theNewArray);
                      Thread.Sleep(mScanThreadWait); //sleep for 50 milliseconds
                      Progress = (float)(y-1 >= 0 ? y-1 : 0) / (float)imheight;
                  }
                  theFS.Close();
                  

                  到目前为止,一切都很好.此代码有效.当前版本(使用 FileStream 和 BinaryWriter)似乎与使用 File.Open 具有相同的选项和正在写入磁盘的 ushort[] 上的 BinaryFormatter 等效(尽管速度较慢,因为复制).

                  So far, so good. This code works. The current version (using FileStream and BinaryWriter) appears to be equivalent (though slower, because of the copy) to using File.Open with the same options and a BinaryFormatter on the ushort[] being written to disk.

                  然后我添加了一个消费者线程:

                  But then I add a consumer thread:

                  FileStream theFS;
                  if (!File.Exists(theFileName)) {
                      //do error handling
                      return;
                  }
                  else {
                       theFS = new FileStream(theFileName, FileMode.Open, 
                          FileAccess.Read, FileShare.Read);
                              //very relaxed file opening
                  }
                  BinaryReader theReader = new BinaryReader(theFS);
                  
                  //gotta do this copying in order to handle byte array swaps
                  //frustrating, but true.
                  byte[] theNewArray = theReader.ReadBytes(
                     (int)(imheight * imwidth * inBase.Progress) * 2);
                  ushort[] theData = new ushort[((int)(theNewArray.Length/2))];
                  Buffer.BlockCopy(theNewArray, 0, theData, 0, theNewArray.Length);
                  

                  现在,theNewArray 的声明可能被破坏了,并且会导致某种读取溢出.然而,这段代码永远不会走得那么远,因为它总是在尝试打开新的 FileStream 时总是中断,并出现 System.IO.IOException 异常,表明另一个进程已打开该文件.

                  Now, it's possible that the declaration of theNewArray is broken, and will cause some kind of read overflow. However, this code never gets that far, because it always always always breaks on trying to open the new FileStream with a System.IO.IOException that states that another process has opened the file.

                  我正在设置 FileAccess 和 FileShare 枚举,如 MSDN 上的 FileStream 文档中所述,但似乎我无法做我想做的事情(即,在一个线程中写入,在另一个线程中读取).我意识到这个应用程序有点不正统,但是当我涉及到实际设备时,我将不得不做同样的事情,但使用 MFC.

                  I'm setting the FileAccess and FileShare enumerations as stated in the FileStream documentation on MSDN, but it appears that I just can't do what I want to do (ie, write in one thread, read in another). I realize that this application is a bit unorthodox, but when I get the actual device involved, I'm going to have to do the same thing, but using MFC.

                  无论如何,我忘记了什么?我想要做的是否可能,因为它在文档中已尽可能指定?

                  In any event, What am I forgetting? Is what I'm wanting to do possible, since it's specified as possible in the documentation?

                  谢谢!嗯嗯

                  推荐答案

                  您的消费者必须指定 FileShare.ReadWrite.

                  Your consumer must specify FileShare.ReadWrite.

                  通过尝试在消费者中以 FileShare.Read 的形式打开文件,您是在说我想打开文件并让其他人同时阅读它"...因为 已经调用失败的写入器,您必须允许与读取器并发写入.

                  By trying to open the file as FileShare.Read in the consumer you are saying "I want to open the file and let others read it at the same time" ... since there is already a writer that call fails, you have to allow concurrent writes with the reader.

                  这篇关于C# 文件读/写文件共享似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:保存字典&lt;String, Int32&gt;在 C# 中 - 序列化 下一篇:强制 ASP.NET 应用程序从 bin 而不是 GAC 加载程序集

                  相关文章

                  最新文章

                  1. <small id='8qbsE'></small><noframes id='8qbsE'>

                    <i id='8qbsE'><tr id='8qbsE'><dt id='8qbsE'><q id='8qbsE'><span id='8qbsE'><b id='8qbsE'><form id='8qbsE'><ins id='8qbsE'></ins><ul id='8qbsE'></ul><sub id='8qbsE'></sub></form><legend id='8qbsE'></legend><bdo id='8qbsE'><pre id='8qbsE'><center id='8qbsE'></center></pre></bdo></b><th id='8qbsE'></th></span></q></dt></tr></i><div id='8qbsE'><tfoot id='8qbsE'></tfoot><dl id='8qbsE'><fieldset id='8qbsE'></fieldset></dl></div>
                    <legend id='8qbsE'><style id='8qbsE'><dir id='8qbsE'><q id='8qbsE'></q></dir></style></legend>
                      <bdo id='8qbsE'></bdo><ul id='8qbsE'></ul>
                    1. <tfoot id='8qbsE'></tfoot>