Detecting the second instance of a program.

BlitzMax Forums/BlitzMax Programming/Detecting the second instance of a program.

levelord(Posted 2009) [#1]
I've searched and searched but can't seem to find any examples showing how to detect multiple instances of a game (BlitzMax program) running. I've seen reference to it being in the Code Archive, but I checked there and under Tools.

I need to know if an instance of the game is already running and, if so, kill the new one and return focus back to the original (in Windows/Vista).

Anyone know where I can find it?

Thanks much!


Nate the Great(Posted 2009) [#2]
ok so to do this you can do something like:

at program startup, read a file in a location like the %TMP% directory (if this file doesn't exist then create it, but if it does exist, terminate the program)

at the end of the program if the file didnt exist and you made one, then delete the one you made :)

problem solved


VIP3R(Posted 2009) [#3]
You can use a mutex for this on Windows.

There's more info in the following thread...

http://www.blitzmax.com/Community/posts.php?topic=55957


xlsior(Posted 2009) [#4]
Nate: Major downside of that workaround is that you can get unexpected results if the program crashes, and doesn't clean up the file it left behind. Try to restart, and it thinks it's already running.

A Mutex would probably be the best approach, or as a quick-and-dirty method you can look at the list of running processes, and see if your application name shows up more than once -- if it does, the program doing to check can assume it was already running and terminate.
Of course that only works if you have a somewhat unique application name, it's bound to lead to problems if your program is called something generic like 'main.exe' or something.


Jim Teeuwen(Posted 2009) [#5]
In linux it's mostly just a matter of checking the process list for the exe name..

$ ps -A | grep "myapp"


This will either show you the Process ID of your app or it will print nothing at all if the app is not running.


xlsior(Posted 2009) [#6]
Here's a working Mutex sample for windows:
- Compile as .exe
- Launch it twice.]
- The Second instance will detect the first instance and terminate itself




Brucey(Posted 2009) [#7]
In wxMax you can simply use a cross-platform friendly wxSingleInstanceChecker object.


Nate the Great(Posted 2009) [#8]
Nate: Major downside of that workaround is that you can get unexpected results if the program crashes, and doesn't clean up the file it left behind. Try to restart, and it thinks it's already running.



true... maybe it would write millisecs() to the file and then it could tell how old the file is... so every 5 seconds the first instance of the program would update the file with the latest millisecs() and if it was greater than 7 seconds old then it would know the app crashed or froze...


xlsior(Posted 2009) [#9]
...or you can do it the proper way, using a mutex, which doesn't require a lock file and semi-continuous harddrive access


levelord(Posted 2009) [#10]
Thanks everyone ;)


Grey Alien(Posted 2009) [#11]
Yep Mutex is the way to go. It's in my framework. You don't have that do you? Talk to BFG about it if you are interested ...