IPC使用共享和全球MMF [英] ipc using Shared and as global MMF

查看:154
本文介绍了IPC使用共享和全球MMF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的目标是将其创建为一个全球性的发言权4000字节长度和创建分区



所以主要凸出是​​MainProj将启动名为$ AppName的$ _sharedMMF

$ b的MMF因此,访问位置是
$ b

然后调试PROJ将访问该$ AppName的$ _sharedMMF

  MainProj->调试器: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的全局实例



所以每次我想访问的分区,我将使用同一个静态的类和方法

  //由主体工程
SharedSetter(SelectedGetter,数据)


1),因为它是由多个线程共享它的作为是有点复杂,但添加分区不是想作为整个设置的确很复杂,这是一个坏主意?



2),这是真的,我不能跳过MMF的创建新实例一步,让它活,只有创造新的访问?



3)没有任何一个有approache执行?


解决方案

 静态无效的主要(字串[] args)
{
FsMomitorIPCCarier数据=新FsMomitorIPCCarier(someData);
IpcAccessorSetting curSrv =新IpcAccessorSetting(IPChannelS.FsMonitor,IpcAccessorThreadNameS.FsmonitorThrd,0,2000);
MMFDepositT FsMonitorSetterDepo = NULL;

{
FsMonitorSetterDepo =新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 + =新的EventHandler< MemoryMappedDataReceivedEventArgs>(FsMonitorSetter_DataReceived);
FsMonitorSetterDepo.StartReader();
}
赶上(例外五)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(MonitorSetterDepo构造函数:+ e.Message);
}

Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine(MonitorSetterDepo现在在线);

变种味精= data.DipositStrVal.StrValue.Val;
Console.WriteLine(数据=+味精);
BOOL退出= FALSE;



{
到Console.ReadLine()(退出!);
如果(string.IsNullOrEmpty(MSG)!)
{
VAR dataDelvr = data.IpcCarierToByteArray();
FsMonitorSetterDepo.Write(dataDelvr);
}
,否则
{
退出= TRUE;
}
//味精=;
}

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

FsMonitorSetterDepo.Close();
FsMonitorSetterDepo = NULL;

}


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?

解决方案

    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;

    }

这篇关于IPC使用共享和全球MMF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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