C# 侦听另一个应用程序中的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listen for events in another application
提问by lomaxx
Suppose I have two applications written in C#. The first is a third party application that raises an event called "OnEmailSent".
假设我有两个用 C# 编写的应用程序。第一个是引发名为“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?
有什么方法可以将第二个应用程序附加到第一个应用程序的实例以侦听“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.
所以为了进一步澄清,我的具体情况是我们有一个用 c# 编写的自定义第三方应用程序,它引发了一个“OnEmailSent”事件。我们可以使用反射器看到事件存在。
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.
我们能想到的最有效的方法是能够使用安德斯建议的某种形式的 IPC 并侦听第三方组件引发的 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.
因为该组件是用 C# 编写的,所以我们正在考虑编写另一个 C# 应用程序,该应用程序可以将自身附加到正在执行的进程中,并且当它检测到 OnEmailSent 事件已引发时,它将执行它自己的事件处理代码。
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.
我更多地考虑了一个场景,例如有人编写了一个独立的应用程序,例如 Outlook,它公开了我想从另一个应用程序订阅的事件。
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.
我想我正在考虑的场景是 .net 调试器以及它如何附加到执行程序集以在代码运行时检查代码。
采纳答案by Anders Sandvig
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.
为了让两个应用程序(不同的进程)交换事件,它们必须就这些事件的通信方式达成一致。有许多不同的方法可以做到这一点,具体使用哪种方法可能取决于体系结构和上下文。进程间这种信息交换的总称是进程间通信(IPC)。有许多执行 IPC 的标准方法,最常见的是文件、管道、(网络)套接字、远程过程调用 (RPC)和共享内存。在 Windows 上,使用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.
我不确定这对于 Windows 上的 .NET/C# 应用程序是如何工作的,但是在本机 Win32 应用程序中,您可以连接到外部进程的消息循环并“监视”它们发送的消息。如果您的程序在调用所需函数时生成消息事件,这可能是一种检测它的方法。
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).
如果您自己实现这两个应用程序,您可以选择使用您喜欢的任何 IPC 方法。网络套接字和更高级别的基于套接字的协议(如 HTTP、XML-RPC 和 SOAP)如今非常流行,因为它们允许您在不同的物理机器上运行应用程序(假设它们通过网络连接)。
回答by Mark Cidade
You can either use remoting or WCF. See http://msdn.microsoft.com/en-us/library/aa730857(VS.80).aspx#netremotewcf_topic7.
您可以使用远程处理或 WCF。请参阅http://msdn.microsoft.com/en-us/library/aa730857(VS.80).aspx#netremotewcf_topic7。
回答by lubos hasko
You can try Managed Spyand for programmatic access ManagedSpyLib
您可以尝试Managed Spy并以编程方式访问ManagedSpyLib
ManagedSpyLib introduces a class called ControlProxy. A ControlProxy is a representation of a System.Windows.Forms.Control in another process. ControlProxy allows you to get or set properties and subscribe to events as if you were running inside the destination process. Use ManagedSpyLib for automation testing, event logging for compatibility, cross process communication, or whitebox testing.
ManagedSpyLib 引入了一个名为 ControlProxy 的类。ControlProxy 是另一个进程中 System.Windows.Forms.Control 的表示。ControlProxy 允许您获取或设置属性并订阅事件,就像您在目标进程中运行一样。使用 ManagedSpyLib 进行自动化测试、兼容性事件日志记录、跨进程通信或白盒测试。
But this might not work for you, depends whether ControlProxy can somehow access the event you're after within your third-party application.
但这可能对您不起作用,这取决于 ControlProxy 是否可以以某种方式访问您在第三方应用程序中关注的事件。
You could also use Reflexil
你也可以使用Reflexil
Reflexil allows IL modifications by using the powerful Mono.Cecil library written by Jb EVAIN. Reflexil runs as Reflector plug-in and is directed especially towards IL code handling. It accomplishes this by proposing a complete instruction editor and by allowing C#/VB.NET code injection.
Reflexil 允许使用由 Jb EVAIN 编写的强大的 Mono.Cecil 库来修改 IL。Reflexil 作为 Reflector 插件运行,特别针对 IL 代码处理。它通过提出一个完整的指令编辑器和允许 C#/VB.NET 代码注入来实现这一点。
回答by Coincoin
What's the nature of that OnEmailSent event from that third party application? I mean, how do you know the application is triggering such an event?
来自该第三方应用程序的 OnEmailSent 事件的性质是什么?我的意思是,你怎么知道应用程序正在触发这样的事件?
If youare planning on doing interprocess communication, the first question you should ask yourself is: Is it really necessary?
如果您打算进行进程间通信,您应该问自己的第一个问题是:真的有必要吗?
Without questioning your motives, if you really need to do interprocess communication, you will need some sort of mechanism. The list is long, very long. From simple WM_DATA messages to custom TCP protocols to very complex Web services requiring additional infrastructures.
不用怀疑你的动机,如果你真的需要进行进程间通信,你将需要某种机制。名单很长,很长。从简单的 WM_DATA 消息到自定义 TCP 协议,再到需要额外基础设施的非常复杂的 Web 服务。
This brings the question, what is it you are trying to do exactly? What is this third party application you have no control over?
这就带来了一个问题,你到底想要做什么?您无法控制的第三方应用程序是什么?
Also, the debugger has a very invasive way of debugging processes. Don't expect that to be the standard interprocess mechanism used by all other applications. As a matter of fact, it isn't.
此外,调试器具有非常侵入性的调试过程方式。不要期望它成为所有其他应用程序使用的标准进程间机制。事实上,事实并非如此。
回答by Mark Cidade
You can implement a similar scenario with SQL Server 2005 query change notifications by maintaing a persistent SqlConnection with a .NET application that blocks until data changes in the database.
您可以使用 SQL Server 2005 查询更改通知来实现类似的场景,方法是使用 .NET 应用程序维护持久的 SqlConnection,该应用程序会阻塞直到数据库中的数据发生更改。
See http://www.code-magazine.com/article.aspx?quickid=0605061.
请参阅http://www.code-magazine.com/article.aspx?quickid=0605061。