如何在WinForms应用程序的另一个流程实例中调用方法? [英] How can I invoke a method within another process instance of my WinForms app?

查看:77
本文介绍了如何在WinForms应用程序的另一个流程实例中调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用Mutex的应用程序,以确保它是系统上运行的应用程序的唯一实例.

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?

通过将消息发送到HWND_BROADCAST,我发现了一些使用RegisterWindowMessage/PostMessage Win32 API的示例,但是我无法使它们正常工作,而且我在其他地方读到,使用HWND_BROADCAST可能很危险.

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?

推荐答案

我以前对此进行过研究-您可以使用内存映射文件,本文对此进行了说明 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

这篇关于如何在WinForms应用程序的另一个流程实例中调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆