ipc使用共享和全球MMF

时间:2016-02-15 23:46:56

标签: c# wpf multithreading ipc memory-mapped-files

使用MMF和C#我在2个进程之间创建了一个共享内存。

我的目标是将其创建为全局说长度为4000字节并创建分区

所以Main proj是" MainProj"将启动名为"$AppName$_sharedMMF"

的MMF

然后" Debugger Proj"将访问"$AppName$_sharedMMF" 所以访问者的位置是:

MainProj->Debugger : readAddr = 0 , writeAddr = 250
Debbugger->MainProj : reafAddr = 250, writeAddr = 0

然后我的解决方案中的第三个可执行文件将是

//setter   getter
MainProj->AnotherExe : readAddr = 251 , writeAddr = 500

//setter    getter
EnotherExe->MainProj : reafAddr = 500, writeAddr = 251

我现在面临的问题是我希望mainProj成为MMF的全局实例

所以每次我想访问分区时我都会使用相同的静态类和方法

//accessed by main project
SharedSetter(SelectedGetter, Data)

1)由于它由多个线程共享,它有点复杂,虽然添加分区并不像整个设置那样复杂,这是一个坏主意吗?

2)我是否真的无法跳过mmf的创建新实例步骤并离开它" Alive"并且只创建新的访问者?

3)是否有人实施了该方法?

1 个答案:

答案 0 :(得分:0)

    static void Main(string[] args)
    {
        FsMomitorIPCCarier data = new FsMomitorIPCCarier("someData");
        IpcAccessorSetting curSrv = new IpcAccessorSetting(IPChannelS.FsMonitor, IpcAccessorThreadNameS.FsmonitorThrd, 0, 2000);
        MMFDepositT FsMonitorSetterDepo = null;
        try
        {
            FsMonitorSetterDepo = new MMFDepositT(curSrv.Channel.ToString(),curSrv.AccThreadName.ToString(), 4096);

            FsMonitorSetterDepo.ReadPosition = curSrv.AccessorSectorsSets.DepoSects.Setter.Read;
            FsMonitorSetterDepo.WritePosition =curSrv.AccessorSectorsSets.DepoSects.Setter.Write;
            Console.WriteLine("MonitorSetterDepo.ReadPosition " + FsMonitorSetterDepo.ReadPosition);
            Console.WriteLine("MonitorSetterDepo.WritePosition " + FsMonitorSetterDepo.WritePosition);

            FsMonitorSetterDepo.DataReceived += new EventHandler<MemoryMappedDataReceivedEventArgs>(FsMonitorSetter_DataReceived);
            FsMonitorSetterDepo.StartReader();
        }
        catch (Exception e)
        {
           Console.ForegroundColor = ConsoleColor.Red;
           Console.WriteLine("MonitorSetterDepo ctor: " + e.Message);
        }

        Console.ForegroundColor = ConsoleColor.Magenta;
        Console.WriteLine("MonitorSetterDepo is now online");

        var msg = data.DipositStrVal.StrValue.Val;
        Console.WriteLine("Data = " + msg);
        bool quit = false;


        while (!quit)
        {
            Console.ReadLine();
            if (!string.IsNullOrEmpty(msg))
            {
                var dataDelvr = data.IpcCarierToByteArray();
                FsMonitorSetterDepo.Write(dataDelvr);
            }
            else
            {
                quit = true;
            }
            //msg = "";
        }

        //DepoTest.statusSet.ForEach(SttM => Console.WriteLine(SttM));

        FsMonitorSetterDepo.Close();
        FsMonitorSetterDepo = null;

    }