C# 进程间通信

发布于 2024-08-25 11:17:44 字数 424 浏览 4 评论 0原文

我正在使用一个应用程序,并且我能够使 C# 脚本在此环境中运行。我可以将任何类型的 DLL 导入到这个环境中。我的问题是我想启用这些脚本之间的通信。由于环境是受控的,并且我无法访问应用程序的源代码,因此我不知道如何执行此操作。

我尝试过的事情:

  • 文件 I/O:只需将我希望每个人读取的消息写入 .txt 文件并让其他人读取它。问题是我需要这个脚本运行得非常快,但这占用了太多时间。

  • nServiceBus:我尝试过这个,但我就是无法让它在我的环境中工作我正在处理。我并不是说它无法完成,只是无法完成它。

有谁知道一种简单的方法来做到这一点,而且速度也相当快?

I'm working with an application, and I am able to make C# scripts to run in this environment. I can import DLLs of any kind into this environment. My problem is that I'd like to enable communication between these scripts. As the environment is controlled and I have no access to the source code of the application, I'm at a loss as to how to do this.

Things I've tried:

  • File I/O: Just writing the messages that I would like each to read in .txt files and having the other read it. Problem is that I need this scripts to run quite quickly and that took up too much time.

  • nServiceBus: I tried this, but I just couldn't get it to work in the environment that I'm dealing with. I'm not saying it can't be done, just that I can't get it done.

Does anyone know of a simple way to do this, that is also pretty fast?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

笑脸一如从前 2024-09-01 11:17:44

您的进程间通信方法应取决于处理每条消息的重要性。

例如,如果流程 A 告诉流程 B 向您的 IT 员工发送一封电子邮件,说明服务器已关闭,那么这非常重要。

但是,如果您正在流式传输音频,则单个消息(数据包)对于应用程序的性能并不重要,并且可以被删除。

如果是前者,您应该考虑使用持久存储(例如数据库)来存储消息,并让每个进程轮询数据库以检索自己的消息。这样,如果一个进程终止或暂时失去与其他进程的通信,它将能够在再次启动时检索丢失的任何消息。

Your method of interprocess communication should depend on how important it is that each message get processed.

For instance, if process A tells process B to, say, send an email to your IT staff saying that a server is down, it's pretty important.

If however you're streaming audio, individual messages (packets) aren't critical to the performance of the app, and can be dropped.

If the former, you should consider using persistent storage such as a database to store messages, and let each process poll the database to retrieve its own messages. In this way, if a process is terminated or loses communication with the other processes temporarily, it will be able to retrieve whatever messages it has missed when it starts up again.

地狱即天堂 2024-09-01 11:17:44

答案很简单:

由于您可以将任何 DLL 导入到脚本中,因此您可以创建一个自定义 DLL,它将以您想要的任何方式实现进程之间的通信:共享内存、命名管道、TCP/UDP。

The answer is simple;

Since you can import any DLL into the script you may create a custom DLL that will implement communication between the processes in any way you desire: shared memory, named pipe, TCP/UDP.

山川志 2024-09-01 11:17:44

即使在同一进程中,您也可以使用某种形式的进程间通信。将您的脚本视为单独的进程,并以这种方式进行通信。

在这种情况下,命名管道可能是一个不错的选择。它们非常快,并且在 .NET 3.5 中相当容易使用。

或者,如果脚本加载到单个 AppDomain 中,您可以使用静态类或单例作为通信服务。但是,如果脚本单独加载,则这可能是不可能的。

You could use a form of Interprocess Communication, even within the same process. Treat your scripts as separate processes, and communicate that way.

Named pipes could be a good option in this situation. They are very fast, and fairly easy to use in .NET 3.5.

Alternatively, if the scripts are loaded into a single AppDomain, you could use a static class or singleton as a communication service. However, if the scripts get loaded in isolation, this may not be possible.

巨坚强 2024-09-01 11:17:44

好吧,由于不了解您的环境细节,我能提供的不多。您正在使用术语“C# 脚本”...我不太确定这意味着什么,因为 C# 通常是一种编译语言。

如果您使用普通的 C#,您是否研究过带有命名管道的 WCF?如果您的程序集在同一台物理计算机上运行,​​您应该能够轻松快速地创建一些使用命名管道绑定托管的 WCF 服务。命名管道在本地上下文中提供了一种简单、高效、快速的消息传输机制。 WCF 本身非常易于使用,并且是 .NET 框架的本机组件。

Well, not knowing the details of your environment, there is not much I can really offer. You are using the term "C# scripts"...I am not exactly sure what that means, as C# is generally a compiled language.

If you are using normal C#, have you looked into WCF with Named Pipes? If your assemblies are running on the same physical machine, you should be able to easily and quickly create some WCF services hosted with the Named Pipe binding. Named pipes provide a simple, efficient, and quick message transfer mechanism in a local context. WCF itself is pretty easy to use, and is a native component of the .NET framework.

小猫一只 2024-09-01 11:17:44

由于您已经有了文件 I/O,您可以通过将其放置在 RAM 磁盘上来获得足够的速度。如果您今天要轮询更改,FileSystemWatcher 可以帮助您的沟通更加灵敏。

Since you already have the File I/O in place you might get enough speed by placing it on a RAM disk. If you are polling for changes today a FileSystemWatcher could help to get your communication more responsive.

绅士风度i 2024-09-01 11:17:44

您可以使用 PipeStream。这比磁盘 IO 快,因为它们是使用主内存完成的。

You can use PipeStream. Which are fast than disk IO as they are done using main memory.

本宫微胖 2024-09-01 11:17:44

XMPP/Jabber 是另一种方法,请参见 jabber.net

XMPP/Jabber is another appraoch take a look at jabber.net.

想你的星星会说话 2024-09-01 11:17:44

另一种简单的方法是在预定义端口上打开 TCP 套接字,从其他进程连接到它并以这种方式进行通信。

Another easy way is to open a TCP Socket on a predefined Port, connect to it from the other process and communicate that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文