ipc using Shared and as global MMF

狂风中的少年 提交于 2019-12-08 07:31:01

问题


using MMF and C# I was creating a Shared Memory between 2 processes.

my aim is to create it as a global say 4000 byte length and create partitions

so Main proj is "MainProj" will start the MMF Named "$AppName$_sharedMMF"

then "Debugger Proj" will Access that "$AppName$_sharedMMF" so the accessor positions are:

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

then the third executable in my solution will be

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

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

the problem I am facing is now that I would like the mainProj to be a Global instance of MMF

so every time I would like to access partition I will use same static class and method

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

1) as it is shared by multiple threads it's bit complicated as is, though adding the partition is not suppose to be complicated as the whole setup does, is this a bad idea ?

2) is it true that I can not skip the create new instance step of the mmf and leave it "Alive" and only create new accessors?

3) does any one have the approache implemented?


回答1:


    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;

    }


来源:https://stackoverflow.com/questions/35421212/ipc-using-shared-and-as-global-mmf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!