簡體   English   中英

C#文件讀/寫fileshare似乎不起作用

[英]C# file read/write fileshare doesn't appear to work

我的問題是基於繼承了我不能做很多的遺留代碼。 基本上,我有一個會產生數據塊的設備。 一個將調用設備來創建該數據塊的庫,由於某種原因我不完全理解並且即使我想要也不能改變,將該數據塊寫入磁盤。

此寫入不是即時的,但最多可能需要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);

現在,NewArray的聲明可能會被破壞,並會導致某種讀取溢出。 但是,這段代碼永遠不會那么遠,因為它始終總是在嘗試使用System.IO.IOException打開新的FileStream時斷言,該System.IO.IOException指出另一個進程已打開該文件。

我正在設置MSDN上的FileStream文檔中所述的FileAccess和FileShare枚舉,但看起來我無法做我想做的事情(即,在一個線程中寫入,在另一個線程中讀取)。 我意識到這個應用程序有點不正統,但是當我得到實際的設備時,我將不得不做同樣的事情,但使用MFC。

無論如何,我忘記了什么? 我想要做什么,因為它在文檔中被指定為可能?

謝謝! MMR

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

通過嘗試在消費者中將文件作為FileShare.Read打開,您說“我想打開文件並讓其他人同時讀取它”...因為已經有一個調用失敗的編寫器,您必須允許與讀者並發寫入。

我沒有時間測試這個,但我認為你可能需要調用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”是罪魁禍首。

我相信Chuck是對的,但請記住,這完全有效的唯一原因是因為文件系統足夠聰明,可以序列化你的讀/寫; 你沒有鎖定文件資源 - 這不是一件好事:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM