一个可执行的preventing多个流程实例 [英] Preventing multiple process instances of a single executable

查看:137
本文介绍了一个可执行的preventing多个流程实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET与C#。

I am using .NET with C#.

我想prevent相同的可执行文件在同一时间运行两个实例,但我不想prevent从另一个文件夹中运行相同的过程。

I want to prevent two instances of the same executable to run at the same time, but I don't want to prevent the same process that is run from another folder.

例如,我有一个可执行文件,是在两个不同的地点:

For example, I have an executable that is in two different locations:

  • C:\ MyProject的\主\ Program.exe文件

  • C:\MyProject\Master\Program.exe

C:\ MyProject的\从\ Program.exe文件

C:\MyProject\Slave\Program.exe

这些是相同的。exe文件,但在两个不同的位置。

These are the same .exe files, but in two different locations.

我想允许Program.exe文件的一个实例是从主文件夹,从文件夹从一个实例运行,但没有两个中的任何。

I want to allow one instance of Program.exe that is run from Master folder and one instance from the Slave folder, but no two of any.

我试图通过检查所有具有相同的名称(Process.GetProcessesByName)进程的数目要做到这一点,但我无法区别这两个。

I've tried to do this by checking the number of processes that have the same name (Process.GetProcessesByName), but then I can't differ the two.

我已经找到了最接近的是从过程得到的模块。

The closest I have found is to get the Modules from the Process.

第一个列表中的exe文件的ProcessModule。但我不知道这是否会永远是第一个就行了。有没有保证的,或者是有给解释问题更好的解决方案?

The first one in the list is ProcessModule of the exe. But I am not sure if this will always be the first one on the list. Is there any guarantee for that, or is there a better solution to the explained problem?

推荐答案

我会用一个全局互斥锁,而不是依赖于程序集名称。另一应用可以是使用例如相同的文件名。

I would use a global mutex instead of relying on the assembly name. Another application may be using the same file name for example.

请参阅例如这样的回答: <一href="http://stackoverflow.com/questions/819773/run-single-instance-of-an-application-using-mutex">Run使用互斥

See e.g. this answer: Run single instance of an application using Mutex

这篇关于一个可执行的preventing多个流程实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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