17

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

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

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

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

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 等效(尽管速度较慢,因为复制)。

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

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 异常,表明另一个进程已打开该文件。

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

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

谢谢!毫米波

4

3 回答 3

38

您的使用者必须指定 FileShare.ReadWrite。

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

于 2008-09-24T02:29:48.837 回答
3

我没有时间对此进行测试,但我认为您可能需要调用 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();

对不起,我没有时间测试这个。我在创建一个与此类似的文件时遇到了问题(尽管不准确),并且缺少“Flush”是罪魁祸首。

于 2008-09-24T02:22:05.687 回答
3

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

于 2008-09-24T02:31:22.640 回答