问题描述
在Windows环境中,我不希望在同一时间运行我的程序的两个实例。
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
- Once per machine
- Once per logon session
- Once per user
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.
这篇关于C / C ++如何判断一个程序已经运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!