进程间通信 [英] Inter-process communication

查看:132
本文介绍了进程间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序:X和Y.
X是主要的应用程序,它处理大量的XML文件。它有十多年的历史,已经使用了六种技术来存储,处理和处理这些XML文件。
Y是一个我正在开发的调试工具,可以处理和显示XML文件更加人性化的形式。基本上,它只是一个样式表的集合,将检测XML格式,如果它识别格式,它将把XML转换为TWebBrowser组件中显示的HTML。

问题:
当Y处于活动状态时,我希望X将其处理的任何XML发送给Y进行显示。但只有当Y运行时!如果Y没有运行,X只是不会做任何事情。
Y的检测需要随时完成,需要快速。我考虑过使用TCP / IP通信,但由于缺少Y引起的延迟太长了。特别是因为很多XML被处理有时。命名管道和类似的基于网络的解决方案也是一样的问题。我需要快速确定Y是否正在运行并可用,如果是,请快速发送XML,然后继续X.
我也考虑使Y成为基于COM的应用程序,或者可能添加基于COM的DLL将允许进程间通信的事件。 DLL解决方案将是有趣的,因为它会向X公开一个方法来上传XML文件,然后将事件发送给Y来处理XML。 这似乎是最好的选择,尽管我也需要检查DLL是否被注册。如果没有,那么X甚至不能调用它!
应用程序X也将被不会收到Y或附加DLL的客户使用,因此在大多数情况下,DLL不会被注册。 (正如我所说,这是为了在调试期间帮助...)

但也许还有其他选择? TCP / IP太慢,COM有点太复杂了。


X和Y将在同一系统上运行。或者只是X将在系统上,Y完全缺失。






关于使用内存映射文件...实际上,我需要记住,大多数时候,Y不会运行,所以MMF会浪费内存。 XML数据在X内的大小可以达到4 MB,因此内存中这个大小的多个块有点过分。它可以用于在X和Y之间发送状态消息,但是内存有时是应用程序X的一个问题。而一个MMF可以连接到一个物理文件,我试图避免完全写入任何临时文件。这是一个很好的解决方案,但我害怕不够好。






有些额外的解释是有序的,我想。应用程序X是一个将要使用几个小时的应用程序,用户可以通过大量的操作来转换大量被处理的XML数据。应用程序X是与几个Web应用程序(REST),Web服务(SOAP)和其他应用程序进行通信的桌面应用程序,其中大部分是通过XML。
应用程序Y仅用于窥视X正在运行的进程。基本上,X工作20分钟,Y弹出。从那时起,X应该开始发送XML到Y,直到Y再次消失或者直到X被终止。在大多数情况下,Y只会运行,以捕获正在运行的任务的一小部分,甚至可能会多次启动。但是我可能正在考虑整个事情在错误的方向。也许X应该是一个服务器,Y注册它...这不是一个真正的问题,当Y找不到X.但是X不能找到Y不能导致延迟或其他问题...

解决方案

你可以更简单地做,因为你只是试图找出一个应用程序是否从另一个运行。只要它们在同一台机器上由同一用户运行,您可以使用X只需使用 FindWindow ()以查看Y是否正在运行。只需确保你给Y一个有意义的名字(在下面的示例中,它是TXMLFormatterForm):

  var 
XMLWindow: HWnd;
begin
XMLWindow:= FindWindow('TXMLFormatterForm',nil);
如果XMLWindow> 0然后
// Y正在运行
end;

只要你确定它是不同的,你也可以使用Y的窗口标题(标题)

  XMLWindow:= FindWindow(nil,'Workshop Alex's XML Formatter'); 


I have two applications: X and Y.
X is the main application and it handles a lot of XML files. It has a history of more than 10 years and half a dozen techniques have been used to store, process and handle these XML files.
Y is a debugging tool that I am developing, which can process and display XML files in a more human-readable form. Basically, it just has a collection of stylesheets that will detect the XML format and if it recognizes the format, it will transform the XML to HTML which is displayed in a TWebBrowser component.

Problem:
When Y is active, I want X to send any XML it proceses to Y for displaying purposes. But only when Y is running! If Y is not running, X just won't do anything.
The detection of Y needs to be done on any moment and needs to be fast. I've considered using TCP/IP communication but the delay caused by a missing Y is just too long. Especially since a lot of XML is processed sometimes. Same problem with named pipes and similar network-based solutions. I need to quickly determine if Y is running and available and if so, send the XML fast and then continue X.
I've also considered to make Y a COM-based application or maybe add a COM-based DLL with events that would allow the inter-process communication. The DLL solution would be interesting since it would expose a method to X to upload an XML file, then send an event to Y to process the XML. This seems to be the best option although I would also need to check if the DLL is registered or not. If not, then X can't even call it!
The application X will also be used by customers who won't receive Y or the additional DLL so in most cases, the DLL will not be registered. (As I said, it's meant to help during debugging...)

But maybe there are other options? TCP/IP is too slow, COM is a bit too complex.


X and Y will be running on the same system. Or just X will be on a system and Y is missing completely.


About using memory-mapped files... While practical, I need to keep in mind that most of the times, Y won't be running thus the MMF will waste memory. XML data can be up to 4 MB in size within X thus having multiple blocks of this size in-memory is a bit overkill. It can be used to send status messages between X and Y but memory is sometimes a bit of a problem with application X. And while a MMF can be connected to a physical file, I'm trying to avoid writing any temporary files altogether.
It's a good solution, but I fear not good enough.


Some additional explanations are in order, I think. Application X is an application which will be used for several hours, with users doing a lot of actions that translate to lots of XML data that gets processed. Application X is a desktop application that communicates with several web applications (REST), web services (SOAP) and other applications and most of this is through XML.
Application Y is just meant to peek inside the processes that X is running. Basically, X is working for 20 minutes and Y pops up. From that moment on, X should start sending XML to Y until Y disappears again or until X is terminated. In most cases, Y will just be running to capture just a small portion of the running tasks and it's possibly even started multiple times. But I might be thinking about the whole thing in the wrong direction. Maybe X should be a server with Y registering to it... It's not a real problem when Y can't find X. But X not finding Y cannot result in delays or other problems...

解决方案

You can do it more simply, since you're just trying to find out if one app is running from another one. As long as they're running on the same machine by the same user, you can have X simply use FindWindow() to see if Y is currently running. Just make sure you give Y a meaningful name (in the sample below, it's TXMLFormatterForm):

var
  XMLWindow: HWnd;
begin
  XMLWindow := FindWindow('TXMLFormatterForm', nil);
  if XMLWindow > 0 then
    // Y is running
end;

You can also use Y's window caption (title), as long as you're sure it's distinct:

XMLWindow := FindWindow(nil, 'Workshop Alex's XML Formatter');

这篇关于进程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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