在两个独立应用程序之间传输对象 (C#)
是否可以将对象从一个应用程序传输到另一个应用程序(在 C# 中)?
我正在使用 CAD API。该 API 的初始化需要几秒钟 (10 - 15)。如果我可以在 App1 中只初始化该对象一次,并在需要时从 App2 中调用它,那就太好了。
有什么想法吗?谢谢!
is it possible to transfer a object from one application to another (in C#)?
I'm working with a CAD API. The initialization of that API takes a few seconds (10 - 15). Would be nice if I could initialize the object only once in App1 and call it up from App2 whenever I need it.
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过序列化您的对象来完成此操作,并且使用任何形式的进程间通信来传输数据,然后在另一端反序列化它。
Windows Communication Foundation 特别适合这种类型的场景,并且可以处理大多数情况为您提供管道。
You can do this by Serializing your object, and using any form of Interprocess Communication to transmit the data, then deserializing it on the other end.
Windows Communication Foundation is specifically suited to this type of scenario, and handles most of the plumbing for you.
虽然里德的建议非常好,但它可能不是唯一值得考虑的可能性。 10-15秒的初始化时间是相当长的。它可能正在初始化一些相当大、复杂的数据结构,这些数据结构可能需要相当长的时间才能序列化和反序列化。如果是这样,可能值得考虑另一种可能性,例如创建一个进程外 COM(或 DCOM)服务器作为 CAD API 的前端,然后让 App1 和 App2 与该服务器一起工作一个地方。这意味着对您发送到 CAD API 或从 CAD API 接收的所有数据进行编组和解组,但具体取决于涉及的数据量以及(特别是)您需要从 App1 与 App2 使用的数据进行切换的频率,可能仍然有较低的开销。
While Reed's suggestion is perfectly good one, it may not be the only possibility worth considering. 10-15 seconds initializing is quite a long time. It may be initializing some fairly large, complex data structures that could take quite a while to serialize and deserialize. If so, it might be worth considering another possibility, such as creating an out-of-process COM (or DCOM) server that acts as a front-end for the CAD API, and then let both App1 and App2 work with that server in one place. This would mean marshalling and unmarshalling all the data you send to/receive from the CAD API, but depending on how much data is involved and (particularly) how often you'd need to switch from its being used by App1 vs. App2, it might still have lower overhead.
在我看来,一种更简单的可能性是使用共享内存。这是Windows下进程间通信的常用机制。您可能需要查看链接的 CodeProject 文章。
An even simpler possibility, in my view, is to use shared memory. This is a common mechanism for interprocess communication under Windows. You may want to take a look at the linked CodeProject article.