问题
Please excuse me if this is a poor question as I am not good with memory mapped files. I am using memory mapped files in my project. I am tracking the files in progress and the file that has already processed in memory mapped files. I have two memory mapped files. In the first memory mapped file, I keep track of processed files and in the second memory mapped file, I keep track of files under processing. So when the processing is complete on a particular file, I make an entry in the first memory mapped file and remove the entry from the second mapped file. Now the problem is that, if all the files are processed, I am disposing the second memory mapped file object. But when the user adds more files for processing, then I am initializing a new object of memory mapped file with the same name as the second memory mapped file name was. And when I try to access this second file, it gives an exception "safe handle has been closed".
Please note: I have to dispose of the second Memory mapped file object.
Please suggest.
FIRST UPDATE Function that writes first memory mapped file
Public void WriteFile()
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
mmf1 = MemoryMappedFile.OpenExisting("Some File Name1");
Class1 Class1obj = new Class1();
string foldername = folderBrowserDialog1.SelectedPath;
Class1obj.CreateMMFFile1(foldername, mmf1, "MMF_IPC1");
}
Class1.RefreshExplorer();
}
Function that writes second memory mapped file:
public void ProcessFiles()
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
mmf = MemoryMappedFile.OpenExisting("Some File Name");
Class1 Class1obj = new Class1();
string foldername = folderBrowserDialog1.SelectedPath;
Class1obj.CreateMMFFile1(foldername, mmf, "MMF_IPC");
}
Class1.RefreshExplorer();
}
Function that append the first file and disposes the second file.
Public void AppendFile()
{
Class1 Class1obj = new Class1();
string foldername = folderBrowserDialog1.SelectedPath;
Class1obj.AppendToMMFFile(mmf1, "MMF_IPC1");
Class1.RefreshExplorer();
mmf.Dispose();
}
Now when the user adds new file for processing the ProcessFiles() function will be called, and I get an exception. Let me know what else I can do to elaborate more on it. The CreateMMFFile1() and CreateMMFFile() are function that writes memory mapped files.
Second UPDATE Here is the AppendToMMFFile function, that append the first MMF file data into second MMF file.
Public void AppendToMMFFile()
{
StringBuilder sb = new StringBuilder();
string str = string.Empty;
try
{
using (var stream = mmf.CreateViewStream())
{
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream, System.Text.Encoding.Unicode);
sb.Append(reader.ReadString());
sb.Append(str + "\r\n");
}
using (var stream = mmf1.CreateViewStream())
{
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream, System.Text.Encoding.Unicode);
writer.Write(sb.ToString());
}
}
catch (Exception ex)
{
Debug.WriteLine("Unable to monitor memory file. " + ex);
}
}
来源:https://stackoverflow.com/questions/9631778/consume-disposed-memory-mapped-file