How can I check to see if my program is already running

BlitzPlus Forums/BlitzPlus Programming/How can I check to see if my program is already running

SuperDan(Posted 2003) [#1]
I am writing a screen saver and trying to check whether it is already running so as to not allow multiple instances.
When the program is started I do this....

If FileType("C:\Program Files\Advanced\Running.sad")=1 Then End
fileout=WriteFile("C:\Program Files\Advanced\Running.sad")
CloseFile (fileout)

Wherever the program is ended, I do this....

DeleteFile "C:\Program Files\Advanced\Running.sad"
End

Is there a safer way to do this because as it is, if the program ends unexpectedly then the file won't be deleted.


RexRhino(Posted 2003) [#2]
Here is one possible avenue to pursue:

Leave the file open when the program is running... don't close the file until the screensaver program is finished.

That means that if another copy of the screensaver is executed, that specific file will be locked (it will already be opened by another program, so it cannot be opened by the second one). If fileout = 0 (I believe, this is off of the top of my head, check the docs yourself), it means the file cannot be opened. If you CAN open it, even if it exists, it means the other screen saver is not running...


Murilo(Posted 2003) [#3]
Yeah we've used RexRhino's solution in the past, and it's worked just fine (in VB6/C++ at least).


JoJo(Posted 2003) [#4]
This is what I did at the beginning of my code.
Works like a charm.

Graphics 800,600,16,3
instance = ReadFile("instance.dat")
Read1 = ReadInt(instance)

If Read1 = 0
	;write 1 to file and continue with game
	instance = WriteFile("instance.dat")
	;SeekFile(instance,0)	
	WriteInt(instance,1)
	Read1 = ReadInt(instance)
	CloseFile(instance)
Else
	;if reaches here that means this game has already been
	;started and this instance should exit and end game
	CloseFile(instance)
	End
EndIf


And at the end of your code, before the End stmt put this.
instance = WriteFile("instance.dat")
Read1 = ReadInt(instance2)
WriteInt(instance1,0)
CloseFile(instance1)



Snarty(Posted 2003) [#5]
Here's an easier solution.
Value%=GetEnv("YOURPROG_ACTIVE")
If Value RuntimeError "See I know everything!"
SetEnvVar "YOURPROG_ACTIVE","1"

Should do the trick.


RexRhino(Posted 2003) [#6]
Wow Snarty, that is a good idea... I am upset that I didn't think of it myself! :)


Snarty(Posted 2003) [#7]
Might find EnvVars are shafted for some reason at the moment though, but that's how I did it for my screensaver way back.


JoJo(Posted 2003) [#8]
Hmmm...Cool deal! I guess I better update my copy of blitz.


soja(Posted 2003) [#9]
It seems to me the problem with using flags (whether they be files or environment variables) is that if something crashes, the flag won't be reset.

Perhaps a better way might be to use a simple Win API call to FindWindow with your window title or something.

Thoughts?


SDF_of_BC(Posted 2003) [#10]
What about using window, the display prop. box,
that wouldn't launch a screen saver twice.


Russell(Posted 2003) [#11]
Can we get hInstance and hPrevInstance from the WinMain() parameters? Oops.. ;)

Seriously, though, there must be a way to get this info.

Russell


RocketGnome(Posted 2003) [#12]

It seems to me the problem with using flags (whether they be files or environment variables) is that if something crashes, the flag won't be reset.



I think you misunderstood the logic that Rex and Snarty were explaining.

The dummy file exists all the time. What Rex suggested you do is lock the file by opening it at the start of the program. If you're unable to open the file, the program must have been started already, so the app that couldn't open the file should terminate immediately. If the program can open the file, it must be the first instance, therefore, it should keep the file locked and continue executing.

If the program crashes, it will release the lock.

Same is true of Snarty's solution. The environment variable will live for as long as the application instance. Therefore, they won't survive a crash either.

There is no "official" way of determining if a program is running or not in the windows api. The above methods described are just as effective without checking for mutexes. (which is probably as close to the system-level of checking this as you can get)


SuperDan(Posted 2003) [#13]
Thanks for that RocketGnome.
It clears up a few things I didn't understand. It's a very good explanation for me as I am only a part timer at BB+.
Taa again.


soja(Posted 2003) [#14]
Yes, thanks.