消耗已处置的内存映射文件 [英] Consume disposed Memory Mapped File

查看:54
本文介绍了消耗已处置的内存映射文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个问题不好,请原谅我,因为我对内存映射文件不满意.我在项目中使用内存映射文件.我正在跟踪正在进行的文件以及已在内存映射文件中处理过的文件.我有两个内存映射文件.在第一个内存映射文件中,我跟踪处理过的文件,在第二个内存映射文件中,我跟踪正在处理的文件.因此,当完成对特定文件的处理后,我在第一个内存映射文件中创建一个条目,并从第二个映射文件中删除该条目.现在的问题是,如果所有文件都已处理,我将处置第二个内存映射文件对象.但是,当用户添加更多文件进行处理时,我将初始化一个内存映射文件的新对象,该对象的名称与第二个内存映射文件的名称相同.当我尝试访问第二个文件时,它给出了一个异常安全句柄已关闭" .

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".

请注意:我必须处理第二个内存"映射文件对象.

请提出建议.

首次更新写入第一个内存映射文件的功能

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

}

现在,当用户添加用于处理 ProcessFiles()函数的新文件时,将出现异常.让我知道我还可以做些什么来详细说明它. CreateMMFFile1() CreateMMFFile()是用于写入内存映射文件的函数.

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.

第二次更新这是AppendToMMFFile函数,它将第一个MMF文件数据附加到第二个MMF文件中.

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

推荐答案

我认为 OpenExisting(String)将打开一个名为MMF ,用于跨进程共享内存.我认为您想要的是光盘上的MMF,就像您从 CreateFromFile(String)中获得的一样.如果您只需要一个新的MMF实例,请将 null 作为名称传递给 OpenExisting(String).

I think OpenExisting(String) will open a named MMF, which is for sharing memory across processes. I think what you want is a MMF from disc like you get from CreateFromFile(String). If you simply need a new MMF instance pass null as name to OpenExisting(String).

否则,也许您应该提出问题,以便我们更好地了解您的用例.

Otherwise maybe you should advance your question, so we can better understand you usecase.

这篇关于消耗已处置的内存映射文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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