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

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

问题描述

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



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



有没有什么办法可以以某种方式将第二个应用程序附加到第一个应用程序的一个实例来监听OnEmailSent事件?






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



我们想要做的是在此组件发送电子邮件时发生其他一些操作。



我们可以想到的最有效的方式是能够使用某种形式的IPC,因为安德斯建议并听取第三方组件引发的OnEmailSent事件。



因为这个组件是用C#编写的,所以我们想着写另外一个可以附加到执行过程的C#应用​​程序,当它检测到OnEmailSent事件已经被提升时,它会执行它自己的事件处理代码。






我可能会缺少一些东西,但是从我了解远程工作原理是什么需要一个定义客户端可以订阅的某种合同的服务器。



我更加想到有人写了一个独立的应用程序,例如展望,暴露事件我想从另一个应用程序订阅。



我想我正在考虑的情况是.net调试器,以及它如何附加到执行程序集

解决方案

为了使两个应用程序(单独的进程)交换事件,他们必须同意如何传达这些事件。有很多不同的方法来做,而使用哪种方法可能取决于架构和上下文。进程之间的这种信息交换的一般术语是进程间通信(IPC) 。存在许多标准的IPC方法,最常见的是文件,管道,(网络)套接字,远程过程调用(RPC)和共享内存。在Windows上,使用窗口消息也很常见。



我不知道如何在Windows上适用于.NET / C#应用程序,但是在本机Win32应用程序中,您可以挂钩到外部进程的消息循环和间谍上他们发送的消息。如果您的程序在调用所需功能时生成消息事件,则可能是一种检测方式。



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

Suppose I have two applications written in C#. The first is a third party application that raises an event called "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.

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


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.

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.

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.

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.

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.

解决方案

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.

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.

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天全站免登陆