如何从另一个 .NET 进程获取对象的句柄?
在 C# 中,我知道如何从代码运行 .NET 可执行文件,并查明可执行文件的实例是否已在运行。我想要做的是,如果实例正在运行,则在不同可执行文件的 C# 代码中获取 Foo
对象的实例。
我有一个Windows应用程序(例如.NET版本的Outlook)。用户可以正常使用该应用程序,但后台运行的是一个进程,用于监视来自第三方系统的 XML 文件。
我需要的是观察者进程启动 .NET 程序(如果它没有运行)(或者获取一个句柄,如果它在运行),然后在 new/ 中的对象实例上调用方法 CreateEmail
现有流程。
In C#, I know how to run a .NET executable from code and also find out if an instance of the executable is already running. What I would like to do is if an instance is already running, obtain an instance of the Foo
object within the C# code of a different executable.
I have a windows application, (e.g..NET version of Outlook). A user can use the application as normal, but also running in the background is a process watching for an XML file from a third party system.
What I need is for the watcher process to start the .NET program if it is not running (or obtain a handle if it is), and then call the method CreateEmail
on the object instance within the new/existing process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 .NET Remoting 激活已运行的应用程序中的对象。
查看以下示例:关于 .NET 远程处理:比较和简化的三个概念< /strong>
You can activate an object in an already running application using .NET Remoting.
Check out the following sample: About .NET Remoting: Three concepts Compared and Simplified
为什么不将 FileSystemWatcher 添加到主应用程序中?也就是说,后台进程除了监视 XML 文件之外什么也不做。
如果这不可行,您可以使用
NamedPipeServerStream
和NamedPipeClientStream
将“命令”从后台进程发送到主应用程序。当主应用程序收到此命令时,它将运行CreateEmail
方法。Why don't you just added the FileSystemWatcher to the main application? That's if the background process is doing nothing else but monitoring for the XML files.
If that's not feasible, you could use the
NamedPipeServerStream
andNamedPipeClientStream
to send a "command" from the background process to the main application. When the main application receives this command, it will run theCreateEmail
method.使用 System.Diagnostics.Process 类。
要运行,您可以
Process.Start("string path");
并列出当前正在运行的进程来执行检查。Use the
System.Diagnostics.Process
class.To run, you can
Process.Start("string path");
and also list current running processes to perform the check.好吧,您可以使用:
返回 Process[]。
查看 Process 类的静态方法,它们非常直观且有用。
Well, you can use:
which returns Process[].
Check out the static methods of the Process class, they are very intuitive and helpful.