C#文件读/写文件共享不会出现工作 [英] C# file read/write fileshare doesn't appear to work

查看:147
本文介绍了C#文件读/写文件共享不会出现工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是基于继承过遗产code大量,我不能做非常关注的。基本上,我有将产生的数据块的装置。这将调用设备来创建数据的块,由于某种原因,我并不完全了解,即使我想不能更改库,该数据块写入磁盘。

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.

在我甚至接触这个传统code,我想用code我完全控制模仿的问题。我使用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.

在制作类的,我有这个code创建数据的随机块:

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();

到目前为止,一切都很好。这code工作。当前版本(使用的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的声明被打破,并会导致某种形式的读取溢出。然而,这code从来没有得到那么远,因为它总是总是一直在尝试与指出另一个进程已打开该文件的System.IO.IOException打开新的FileStream打破。

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和文件共享枚举为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?

谢谢!
MMR

Thanks! mmr

推荐答案

您的消费者必须指定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#文件读/写文件共享不会出现工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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