C/C++ 如何判断程序是否已经在运行?
在 Windows 环境中,我不希望程序的两个实例同时运行。
相关
In a Windows environment, I don't want two instances of my program running at the same time.
Related
Is using a Mutex to prevent multiple instances of the same program from running safe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果这是您的程序,那么 Windows 下的最短版本:
如果您需要的只是一个简单的检查,则无需变得疯狂的复杂...
If it's your program, then the shortest possible version under windows:
No need to get crazy complex, if all you needed was a simple check...
我认为在继续之前你需要考虑一下你的场景。 对于“多次运行同一个程序”有许多不同的解释。 例如,
所有这些都有不同但相似的解决方案。
最容易描述的是每台机器。 在本例中,您想要创建一个命名的互斥体。 每个程序在启动时都必须获取该互斥锁,如果成功,它们就会在进程生命周期内运行并保留该互斥锁。 否则,其他程序正在运行,它们会立即退出。
不幸的是,这种方法也有其缺点。 如果我想搞乱你的程序,我可以创建一个同名的互斥体。 这将阻止您的程序运行任何实例,因为它们无法判断谁持有互斥锁,只是知道有什么东西持有互斥锁。
I think you need to consider your scenario a bit before going forward. There are many different interpretations of "running the same program" more than once. For instance do you
All of these have different, albeit similar, solutions.
The easiest one to describe is the per machine. In this case you want to create a named Mutex. One startup every program must obtain this mutex, if they are successful they run and hold onto the Mutex for the duration of the process lifetime. Otherwise some other program is running and they exit immediately.
Unforunately this approach also has its drawbacks. If I want to mess up your program, I can create a mutex with the same name. This will prevent your program from running any instance because they are unable to tell who holds the Mutex, just that something is holding the mutex.
您可以在应用程序的第一个实例启动时创建互斥锁。 为了防止第二次发生,您所需要做的就是检查是否正在使用互斥体。
实际上,有人提出了关于为此目的使用互斥体的问题 在这里查看 JaredPar 的答案。
注意:如果您希望“一个实例”仅在用户会话中应用(而不是适用于所有用户),则可以使用本地互斥锁
You could create a mutex when the first instance of your application starts. To prevent a second instance all you'd need to do is check if the mutex is being used.
Actually there was a question raised about using mutexes for this purpose here check out JaredPar's answer.
Note: You can use a local mutex if you want the "one instance" to apply only within a user's session (instead of for all users)
最好的方法是使用互斥锁。 请参阅使用互斥对象。
The best way is to use a mutex. See Using Mutex Objects.
另一种简单的解决方案是创建一个适当唯一的全局命名事件(可能是 GUID 字符串),然后在启动时检查其是否存在。 如果存在,则您的应用程序的实例已经启动。 如果没有,您已自动创建事件并可以继续运行,例如:
An alternative simple solution is to create a suitably unique global named event (possibly a GUID string) then check for its existence on startup. If it exists then an instance of your app has already been started. If not, you've automatically created the event and can continue to run, e.g.:
这是我使用 boost.interrprocess 编写的类,我用它在 GUI 和 CLI 版本之间进行同步。
您可能会发现它很有用:
用法很简单:
因此您可以轻松限制并行进程的数量。
this is a class I scripted using boost.interrprocess, I use it to sync between the GUI and CLI versions.
You might find it useful:
the usage is simple:
so you can easily limit how many processes in parallel.
当您使用 Qt 时,您可以下载 QtSingleApplication 组件。
When you use Qt you can download the QtSingleApplication component.
正如其他人所建议的那样,使用互斥体。
MS 的 CreateMutex() 文档 有一个许多有用的信息,并专门解决了使用互斥体来防止程序的多个实例运行的情况。 特别是:
bInitialOwner = FALSE
调用CreateMutex()
,然后调用等待函数(例如WaitForSingleObject()
)以确保只有一个实例获取互斥体。Use a mutex, as others have suggested.
That CreateMutex() documentation from MS has a lot of useful information, and specifically addresses the case of using mutexes for preventing more than one instance of a program from running. In particular:
CreateMutex()
withbInitialOwner = FALSE
, then call a wait function (e.g.WaitForSingleObject()
) to ensure that just one instance acquires the mutex.在程序启动时,您可以枚举计算机上运行的进程
然后如果你发现你已经在运行了,退出
At the startup of your program, you can enumerate the processes running on your machine
Then if you see that you're already running, quit
您可以检查窗口类是否已经注册。 查看此 MSDN 条目。
You could check if window class is already registered. Take a look at this MSDN entry.