侦听另一个应用程序中的事件 [英] Listen for events in another application

查看:24
本文介绍了侦听另一个应用程序中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个用 C# 编写的应用程序.第一个是引发名为OnEmailSent"的事件的第三方应用程序.

Suppose I have two applications written in C#. The first is a third party application that raises an event called "OnEmailSent".

第二个是我编写的自定义应用程序,我想以某种方式订阅第一个应用程序的OnEmailSent".

The second is a custom app that I've written that I would like to somehow subscribe to the "OnEmailSent" even of the first application.

有什么办法可以将第二个应用程序附加到第一个应用程序的实例以侦听OnEmailSent"事件?

Is there any way that I could somehow attach the second application to an instance of the first application to listen for "OnEmailSent" event?

所以为了进一步澄清,我的具体情况是我们有一个用 c# 编写的自定义第三方应用程序,它引发了OnEmailSent"事件.我们可以使用反射器看到事件存在.

So for further clarification, my specific scenario is that we have a custom third party application written in c# that raises an "OnEmailSent" event. We can see the event exists using reflector.

我们想要做的是当这个组件发送电子邮件时发生一些其他的动作.

What we want to do is have some other actions take place when this component sends an email.

我们能想到的最有效的方法是能够使用安德斯建议的某种形式的 IPC 并监听第三方组件引发的 OnEmailSent 事件.

The most efficient way we can think of would be to be able to use some form of IPC as anders has suggested and listen for the OnEmailSent event being raised by the third party component.

因为该组件是用 C# 编写的,我们正在考虑编写另一个 C# 应用程序,该应用程序可以将自身附加到正在执行的进程中,并且当它检测到 OnEmailSent 事件已引发时,它将执行它自己的事件处理代码.

Because the component is written in C# we are toying with the idea of writing another C# application that can attach itself to the executing process and when it detect the OnEmailSent event has been raise it will execute it's own event handling code.

我可能遗漏了一些东西,但根据我对远程处理工作原理的理解,需要一个服务器来定义客户端可以订阅的某种合同.

I might be missing something, but from what I understand of how remoting works is that there would need to be a server defining some sort of contract that the client can subscribe to.

我更多地考虑了这样一种情况:有人编写了一个独立的应用程序,例如 Outlook,它公开了我想从另一个应用程序订阅的事件.

I was more thinking about a scenario where someone has written a standalone application like outlook for example, that exposes events that I would like to subscribe to from another application.

我想我正在考虑的场景是 .net 调试器以及它如何附加到执行程序集以在代码运行时检查代码.

I guess the scenario I'm thinking of is the .net debugger and how it can attach to executing assemblies to inspect the code whilst it's running.

推荐答案

为了让两个应用程序(单独的进程)交换事件,它们必须就这些事件的通信方式达成一致.有许多不同的方法可以做到这一点,具体使用哪种方法可能取决于体系结构和上下文.进程间这种信息交换的通用术语是进程间通信 (IPC).存在许多执行 IPC 的标准方法,最常见的是文件、管道、(网络)套接字、远程过程调用(RPC) 和共享内存.在 Windows 上,使用 窗口消息也很常见.

In order for two applications (separate processes) to exchange events, they must agree on how these events are communicated. There are many different ways of doing this, and exactly which method to use may depend on architecture and context. The general term for this kind of information exchange between processes is Inter-process Communication (IPC). There exists many standard ways of doing IPC, the most common being files, pipes, (network) sockets, remote procedure calls (RPC) and shared memory. On Windows it's also common to use window messages.

我不确定这对于 Windows 上的 .NET/C# 应用程序是如何工作的,但在本机 Win32 应用程序中,您可以 挂钩外部进程的消息循环并窥探"它们发送的消息.如果您的程序在调用所需函数时生成消息事件,这可能是一种检测它的方法.

I am not sure how this works for .NET/C# applications on Windows, but in native Win32 applications you can hook on to the message loop of external processes and "spy" on the messages they are sending. If your program generates a message event when the desired function is called, this could be a way to detect it.

如果您自己实现这两个应用程序,您可以选择使用您喜欢的任何 IPC 方法.网络套接字和更高级别的基于套接字的协议(如 HTTP、XML-RPC 和 SOAP)如今非常流行,因为它们允许您在不同的物理机器上运行应用程序(假设它们通过网络连接).

If you are implementing both applications yourself you can chose to use any IPC method you prefer. Network sockets and higher-level socket-based protocols like HTTP, XML-RPC and SOAP are very popular these days, as they allow you do run the applications on different physical machines as well (given that they are connected via a network).

这篇关于侦听另一个应用程序中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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