检测程序的运行实例,并向其传递的信息 [英] Detecting a running instance of a program and passing it information

查看:160
本文介绍了检测程序的运行实例,并向其传递的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,什么是最可靠的方法来检测,如果在同一个程序的实例已在运行?此外,什么是命令行参数传递到已经运行的情况下的最佳方法(如打开一个特定的文件)?

In .NET, what is the most reliable way to detect if an instance of the same program is already running? Further, what is the best way to pass command line arguments to the already-running instance (e.g. to open a specific document)?

推荐答案

似乎有三个著名实施.NET程序单实例应用程序的方法:工艺方法,互斥的方法,和Visual Basic方法

There appears to be three well-known ways of implementing single-instance applications in .NET programs: the process method, the Mutex method, and the Visual Basic method.

迈克尔·温顿讨论了他的暗示前两者,确保只有单个实例.NET应用程序运行

Michael Covington discusses the first two in his hint, Ensuring that only a single instance of a .NET application is running.

有一个 $ C $的CProject文章,讨论互斥方法更详细,包括获取现有的应用程序的窗口句柄,这样你就可以(在某种程度上)与之沟通。

There's a CodeProject article that discusses the Mutex method in more detail, including getting the window handle of the existing application so that you can then (somehow) communicate with it.

另外 $ C $的CProject文章介绍如何使用类在Microsoft.VisualBasic命名空间(这是从C#访问)实行单一实例应用程序。

Another CodeProject article describes how to use classes in the Microsoft.VisualBasic namespace (which is accessible from C#) to implement a single-instance application.

我一点也不熟悉的第三种方法。前两种的,它们都工作,但正如指出的链接的文章,该互斥方法似乎更加健壮。我不知道为什么互斥被选定为使用指定的对象。任何命名的同步对象(的EventWaitHandle,互斥,信号灯等)会工作。

I'm not at all familiar with the third method. Of the first two, they both work but as is pointed out in the linked articles, the Mutex method seems more robust. I don't know why Mutex was selected as the named object to use. Any of the named synchronization objects (EventWaitHandle, Mutex, Semaphore, etc.) would work.

与现有的应用程序presents更多的问题交流。我建议你​​考虑使用命名管道,如果你需要做的就是发送一个相对较小的消息,正在运行的应用程序。请查看 System.IO.Pipes.NamedPipeClientStream 了解更多信息。

Communicating with the existing application presents more of a problem. I'd suggest you look into using named pipes, if all you need to do is send a relatively small message to the running application. Check out System.IO.Pipes.NamedPipeClientStream for more information.

我也应该注意,如果你担心安全,你需要确保任何通信方式使用。具有足够的权限的任何应用程序将能够通过命名管道将信息发送到您运行的应用程序。

I should also note that if you're worried about security, you'll need to secure whatever communications method you use. Any application that has sufficient permissions will be able to send information to your running application through the named pipe.

这篇关于检测程序的运行实例,并向其传递的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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