Recommended way of sharing an object between C# processes

和自甴很熟 提交于 2019-12-07 08:12:34

问题


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.

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?).

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.

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.

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.

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.

Update - Some more information

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).

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.


回答1:


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.

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




回答2:


In my company we are using a solution that is a Combination of XML object Serialisation and the FileSytemWatcher that is more or less the same as named Pipes but it is fast and its working well.




回答3:


I think WCF with NetNamedPipeBinding would be an easier/better option.

Example here.



来源:https://stackoverflow.com/questions/21031138/recommended-way-of-sharing-an-object-between-c-sharp-processes

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