C#文件读/写文件共享不';似乎不起作用

C#文件读/写文件共享不';似乎不起作用,c#,binary,fileshare,C#,Binary,Fileshare,我的问题是基于继承了大量我不能做太多的遗留代码。基本上,我有一个设备,将产生一个数据块。一个调用设备来创建数据块的库,由于某种原因,我不完全理解,即使我想更改也无法更改,它会将该数据块写入磁盘 此写入不是即时的,但可能需要90秒。在这段时间里,用户希望获得正在生成的数据的部分视图,因此我希望有一个消费者线程,它读取另一个库正在写入磁盘的数据 在我接触这个遗留代码之前,我想用我完全控制的代码来模拟这个问题。我使用C#,表面上是因为它提供了很多我想要的功能 在producer类中,我使用以下代码创建

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

此写入不是即时的,但可能需要90秒。在这段时间里,用户希望获得正在生成的数据的部分视图,因此我希望有一个消费者线程,它读取另一个库正在写入磁盘的数据

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

在producer类中,我使用以下代码创建一个随机数据块:

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();
现在,有可能是数组的声明被破坏了,并将导致某种读取溢出。但是,这段代码永远不会走得那么远,因为它总是在尝试打开新文件流时中断,并出现System.IO.IOException,该异常表示另一个进程已打开该文件

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

无论如何,我忘记了什么?我想做的事情可能吗,因为文档中尽可能地指定了它

谢谢!
mmr

我还没有时间测试这个,但是我认为您可能需要调用BinaryWriter的Flush方法

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;
    theBinaryWriter.Flush();
}
theFS.Close();
FileStream offs=new FileStream(this.ScannerRawFileName,
FileMode.OpenOrCreate、FileAccess.Write、FileShare.Read);
//请注意,我需要能够在别处阅读这篇文章。。。
BinaryWriter theBinaryWriter=新的BinaryWriter(OFFS);
int y,x;
对于(y=0;y=0?y-1:0)/(浮动)imheight;
二进制writer.Flush();
}
offs.Close();

对不起,我没有时间测试这个。我在创建的一个文件中遇到了一个问题,该文件与此类似(虽然不完全相同),并且缺少“刷新”是罪魁祸首。

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


通过尝试以FileShare.Read的形式打开该文件,您会说“我想打开该文件,让其他人同时读取”。。。由于已经有一个写入程序调用失败,您必须允许与读卡器并发写入。

我相信Chuck是对的,但请记住,这一切工作的唯一原因是因为文件系统足够智能,可以序列化您的读/写;您没有对文件资源进行锁定—这不是一件好事:)

很公平—但请记住,我必须对几乎肯定没有任何文件锁定的遗留代码执行此操作(编写此代码的人会看着您,好像您是疯子,因为您建议存在这样的东西)。所以,如果它因为这个而断裂,我最好模仿那些断裂。
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;
    theBinaryWriter.Flush();
}
theFS.Close();