无法从内存映射文件读取 [英] Trouble reading from Memory Mapped File

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

问题描述

我试图在我的应用程序(特别是Windows服务)中实现内存映射文件,然后使用C#格式从服务写入的MMF中读取信息.不幸的是,我似乎无法从MMF中读取该表格,更重要的是,该表格似乎找不到该服务创建的MMF.下面是概述我正在做什么的代码段,有人可以看到我做错了什么,或者可以向我指出更好的方向吗?

I am trying to implement a Memory Mapped File within my application (specifically a Windows Service), and then use a C# form to read from MMF the service writes to. Unfortunately i cannot seem to get the form to read anything from the MMF, more importantly it seems that the form never finds the MMF created by the Service. Below are code snippets that outline what im doing, can anyone see what I am doing wrong or be able to point me in a better direction?

服务:

private MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("AuditStream", 1024 * 1024);
private Mutex mutex = new Mutex(false, "MyMutex");

byte[] msg = new byte[1];
var view = mmf.CreateViewStream(0, 1);
byte[] rmsg = new byte[1];

for (int i = 0; i < 400; i++)
{
     mutex.WaitOne();
     for (int j = 0; j < msg.Length; j++)
     {
          msg[j] = (byte)i;
     }

     view.Position = 0;
     view.Write(msg, 0, bufferSize);

     //the next 3 lines verify that i wrote to the mmf and can potentially read from it
     //These are just for testing
     view.Position = 0;
     view.Read(rmsg, 0, 1);
     Log.Error("Finished MMF", rmsg[0].ToString());

     mutex.ReleaseMutex();
 }

表格:

private MemoryMappedFile mmf;
private Mutex mutex;
Thread t = new Thread(MmfMonitor);
t.Start();

private void MmfMonitor()
    {

        byte[] message = new byte[1];
        while(!quit)
        {
            try
            {
                **mmf = MemoryMappedFile.OpenExisting("AuditStream");**
                mutex = Mutex.OpenExisting("MyMutex");
                var view = mmf.CreateViewStream(0, 1);

                mutex.WaitOne();
                view.Position = 0;
                view.Read(message, 0, 1);
                Invoke(new UpdateLabelCallback(UpdateLabel), message[0].ToString());
                mutex.ReleaseMutex();
            }catch(FileNotFoundException)
            {
                **//The AuditStream MMF is never found, and therefore doesnt every see the proper values**
            }
        }
    }

此外,当服务处于运行"状态时,MMF应该始终具有一个句柄,并且不应被垃圾收集器收集;

Additionally, while the service is 'Running', the MMF should always have a handle and shouldnt get collected by the garbage collector;

推荐答案

该服务在另一个会话中运行,即著名的会话0". Windows对象位于与该进程的会话相关联的名称空间中,因此您的窗体看不到该服务所使用的会话中创建的对象.

The service runs in a different session, the famous "session 0". Windows objects live in a namespace that's associated with the session of the process, so your form can't see the object created in the session used by the service.

您必须在mmf名称之前加上Global\才能在全局名称空间中创建和访问该对象.

You must prepend Global\ to the mmf name to create and access the object in the global namespace.

因此在服务中:

mmf = MemoryMappedFile.CreateOrOpen(@"Global\AuditStream", ...)

,其格式为:

mmf = MemoryMappedFile.OpenExisting(@"Global\AuditStream");

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

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