在C#进程之间共享对象的推荐方法 [英] Recommended way of sharing an object between C# processes

查看:691
本文介绍了在C#进程之间共享对象的推荐方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了2个C#应用程序及其优缺点之间有关IPC的许多不同知识,但是对于我的用例,我还没有感觉到令人满意的答案.

I have read numerous different things about IPC between 2 C# applications and their pros and cons, but don't feel like I have reached a satisfactory answer yet for my use case.

我有一个已经存在并且会经常变化的对象(我正在尝试将我的工具附加到游戏上,并使用它来调试使用该工具创建的元素).结果,我不认为序列化是合适的,因为我会无缘无故地每秒对对象进行60次序列化/反序列化.结果,无法进行配管(或者我在这里错过了什么吗?).

I have an object that already exists that will change frequently (I am trying to attach my tool to a game and use it to debug elements created with the tool). As a result, I don't beleive serialisation is appropriate as I would essentially be serialising/de-serialsing the object 60 times a second for no good reason. As a result, piping is not possible (or am I missing something here?).

由于游戏在Unity中运行,我仅限于.NET 3.5技术,因此不能使用新的.NET4共享内存类.

As the game is running in Unity, I am limited to .NET 3.5 technologies so can't use the new .NET4 shared memory class.

因此,.NET远程处理似乎是必经之路.这不是理想的选择-我不需要网络支持,我要共享的对象在内存中,没有真正的理由使用代理和发送消息来更改它的开销.

So it seemed like .NET remoting is the way to go. It is less than ideal - I have no need for network support, the object I want to share is in memory, no real reason for the overhead of using proxies and sending messages to change it.

但是,每个人本教程链接似乎不太好-源代码无法编译,并且在我编译时崩溃了.本教程本身未引用Cache类,该类似乎很重要,即使使用源代码,我也看不到它如何适合我的应用程序.有更好的资源吗?这真的是最好的方法吗?

However, this tutorial which everyone links to doesn't seem to be good - the source code doesn't compile and when I got it to compile it crashed. The tutorial itself makes no reference to the Cache class which seems central and I can't see, even with the source code, how it would fit in with my application. Is there a better resource? Is this really the best approach.

最后,我剩下的工作就是使用C ++进行插值,以使用非托管功能来创建共享内存并将对象移入其中.在我掉进那个兔子洞之前,我想确认一下它们确实不是更好的方法.

Finally I am left with interpolating with C++ to use the unmanaged functionality of creating a shared memory and moving the object into it. Before I go down that rabbit hole, I wanted to confirm they really isn't a better way.

更新-一些更多信息

目前仅尝试尝试2个控制台应用程序.但是,最后我有一个C#/Winforms应用程序(如果有帮助,此 is 是.NET 4.0),它将连接到Unity进程(显然我无法控制).我有一个供该工具和Unity使用的DLL.我将在其中创建一个类,该类将允许工具访问对象(例如,如果我可以使用管道,则可以从Unity中调用该类来创建管道,然后从工具连接到管道).

At the moment just trying things out so 2 console applications. However, in the end I have one C#/Winforms application (this is is .NET 4.0 if that helps) which I will connect to the Unity process (which obviously I have no control over). I have a DLL which is used by both the tool and Unity. I was going to have a class in that which would allow the tool to access the objects (e.g. if I could use pipes, from Unity I'd call into this class to create the pipe and then connect to the pipe from the tool).

对象本身实质上是一个有限状态机,其描述是从XML文件加载的.可以从极少量的数据中重新创建对象.但是,我宁愿避免手动编码使用某种事件/消息系统的解决方案,以使对象在活动状态等方面保持同步.

The objects themselves represent essentially a finite state machine whose description is loaded from an XML file. It would be possible to recreate the object from a very minimal amount of data. However, I would rather avoid hand coding a solution that uses some kind of event/message system to keep the objects in sync with regard to which state is active etc.

推荐答案

我们在这里讨论什么样的数据?如果它是符号/指标,则可以将其定界并存储在内存映射文件中并共享.

What kind of data are we talking about here? If it is symbols / metrics, it could be delimited and stored in a memory mapped file and shared.

由于您使用的是3.5版本,因此无法直接使用MMF文件,但是FileMap应该可以为您工作

Since you're on 3.5, you can't use the MMF file directly, but FileMap should work for you

https://github.com/tomasr/filemap/tree/master

这篇关于在C#进程之间共享对象的推荐方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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