PDA

View Full Version : ensure that only one instance of program is running



alireza.mirian
5th January 2011, 18:08
Hi
In many cases you may want to ensure that only one instance of your program is running. in another word,if an instance of program is running and the user tries to run a new instance,you want to found that and exit second program and maybe show an appropriate message. my question is how to implement this.
I thought about some ways like register a key in windows registry or any other thing that could use as a flag that indicate that program is running or not( by setting this flag when program runs and unset it on exiting the program) but it's a bad idea when the user kills the process or program exit with any bad status.

if you know the usual way to implement that feature, please help me :)

tbscope
5th January 2011, 18:21
One cross platform technique but flawed in some contexes.

Create a file with the PID of the program.

1. check if this file exists.
If it does, check the PID, is it running or not? If it is running, close the new application.
If for some reason the file didn't get erased, the PID will most likely not be running (a flaw) and the file can be updated with the new PID.

2. When the program starts, write the PID to a file.

3. When the program exits, remove that file.

Or set an environment variable.

alireza.mirian
5th January 2011, 18:36
One cross platform technique but flawed in some contexes.

Create a file with the PID of the program.

1. check if this file exists.
If it does, check the PID, is it running or not? If it is running, close the new application.
If for some reason the file didn't get erased, the PID will most likely not be running (a flaw) and the file can be updated with the new PID.

2. When the program starts, write the PID to a file.

3. When the program exits, remove that file.

Or set an environment variable.

thanks alot. this is like my "flag" solution and checking PID that U suggest will solve the problem of my solution. this solve the problem but my question is that, is this the formal and usual way to solve this? I ask it because it seems a little unsecure. if anyone removes the file, he/she can have more than one instance of program and if its critical to not have more than one instance, it may make mistake.

javimoya
5th January 2011, 18:41
http://qt.nokia.com/products/qt-addons/solutions-archive/
http://doc.qt.nokia.com/solutions/4/qtsingleapplication/qtsingleapplication.html#details

:-)

alireza.mirian
5th January 2011, 18:53
http://qt.nokia.com/products/qt-addons/solutions-archive/
http://doc.qt.nokia.com/solutions/4/qtsingleapplication/qtsingleapplication.html#details

:-)

wow, that was EXACTLY what i want! :D thank U very very much ;)
but when I tried it, complier says:"Cannot open include file:'QtSingleApplication':No such file or directory" :(


wow, that was EXACTLY what i want! :D thank U very very much ;)
but when I tried it, complier says:"Cannot open include file:'QtSingleApplication':No such file or directory" :(

got it, got it... ;) thank U