如何在 WinForms 应用程序的另一个流程实例中调用方法?
我正在开发一个使用互斥体的应用程序,以确保它是系统上运行的应用程序的唯一实例。
当应用程序的另一个实例尝试启动时,我希望有一个方法在原始实例中运行。
我可以从应用程序的另一个实例调用应用程序中的特定方法吗?
我发现了一些使用 RegisterWindowMessage/PostMessage Win32 API 通过将消息发送到 HWND_BROADCAST 的示例,但我无法让它们工作,并且我在其他地方读到使用 HWND_BROADCAST 可能很危险。
有没有更好的方法来做到这一点,而不涉及应用程序需要在特权模式下运行?
I am working on an application that uses a Mutex to ensure that it is the only instance of the application running on the system.
When another instance of the application attempts to start, I want a method to run in the original instance.
Can I invoke a specific method in my application from another instance of the application?
I've found some examples using RegisterWindowMessage/PostMessage Win32 APIs by sending the message to HWND_BROADCAST, but I couldn't get them to work, and I've read elsewhere that using HWND_BROADCAST can be dangerous.
Is there a better way to do this that doesn't involve the app needing to be run in privileged mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我写的一个小帮手。
要使用它:
从另一个进程:
请注意,它使用 PipeListener 的全名作为管道的默认名称。如果您需要比这更离散,请使用采用管道名称的构造函数重载。
这是课程:
Here's a little helper I wrote.
To use it:
From another process:
Note that it uses the full name of the PipeListener as the default name of the pipe. If you need to get more discrete than that, use the constructor overload that takes a pipe name.
Here's the class:
我之前对此进行过研究 - 您可以使用内存映射文件,如本文 中所示http://www.codeproject.com/KB/cs/singleinstanceapplication.aspx,或者您可以按照我的做法(简单的方法)并利用 vb.net 功能(具体来说,可以让您制作单实例应用程序并调用当前正在运行的实例中传递命令行参数的方法[因此您可以使用它来调用应用程序中的方法])。我知道在 C# 中使用 VB 类听起来有点糟糕,但这是最抽象、最简单的方法。相关文章的链接 - http://www.codeproject.com/KB/cs/CSSIApp .aspx,http://msdn.microsoft.com/en-us/magazine/cc163741.aspx
I've done research on this before - you can use a memory mapped file, demonstrated in this article http://www.codeproject.com/KB/cs/singleinstanceapplication.aspx, or you can do what I did (the easy way) and take advantage of vb.net features (specifically, one that lets you make single instance apps and calls a method in the currently running instance that passes on command line args [so you could use it to invoke the method in your application]). I know using VB classes in C# sounds a bit poor but it's the most abstract and easy way. Link to the relevant articles - http://www.codeproject.com/KB/cs/CSSIApp.aspx, last part of http://msdn.microsoft.com/en-us/magazine/cc163741.aspx