如何从另一个 .NET 进程获取对象的句柄?

发布于 2024-08-07 23:54:35 字数 325 浏览 6 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独自唱情﹋歌 2024-08-14 23:54:35

您可以使用 .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

煞人兵器 2024-08-14 23:54:35

为什么不将 FileSystemWatcher 添加到主应用程序中?也就是说,后台进程除了监视 XML 文件之外什么也不做。

如果这不可行,您可以使用 NamedPipeServerStreamNamedPipeClientStream 将“命令”从后台进程发送到主应用程序。当主应用程序收到此命令时,它将运行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 and NamedPipeClientStream to send a "command" from the background process to the main application. When the main application receives this command, it will run the CreateEmail method.

寒尘 2024-08-14 23:54:35

使用 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.

聊慰 2024-08-14 23:54:35

好吧,您可以使用:

Process.GetProcessesByName(<process Name here>);

返回 Process[]。

查看 Process 类的静态方法,它们非常直观且有用。

Well, you can use:

Process.GetProcessesByName(<process Name here>);

which returns Process[].

Check out the static methods of the Process class, they are very intuitive and helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文