Create only one instance of a program?

Blitz3D Forums/Blitz3D Programming/Create only one instance of a program?

Guy Fawkes(Posted 2014) [#1]
Hi all. Is there anything floating around that can allow for a program to be opened only ONE time and no more in order to save memory?

Thank You so kindly! =)

Sincerely,

~GF


wmaass(Posted 2014) [#2]
Maybe use api_FindWindow().


Guy Fawkes(Posted 2014) [#3]
Well if they renamed the file, they would be able to open a 2nd instance. Which is what I'm trying to prevent.


H&K(Posted 2014) [#4]
Create a time stamped "is an instance running" file


Guy Fawkes(Posted 2014) [#5]
time stamped is an instance running file?

Can you elaborate a little more please?

Thank You so kindly! =)

Sincerely,

~GF


H&K(Posted 2014) [#6]
Open/create a file, write the time to it, re-write to it every min (say).

When an instance starts it reads/looks for file, and if the time is less than a min (say) it know an instance is either running or just crashed.

Fair enough you have to wait a min (say) after a crash, but ALL one instance programs have that restart delay crap, just let the restarted instance hang about for a min (say) and then recheck. If it passes, It will just look like the program is doing some "crash fixing rubbish", and if it fails then so what you made them wait a min (say) before you give the one instance message.

If you have the cpu time you could check every cycle, but that seems a waste of cycle time to me when once every couple of seconds would appear invisible depending on the start-up speed of your program

Ps The files location needs to be independent of programme name/location
PPS If you really wanted to you could also "crash" the original instance, to discourage this sort of thing.


Rroff(Posted 2014) [#7]
If someone wants to open multiple instances for whatever reason they will - even if it means running one under another name under another user so the other instance can't interfere with it etc.

If they've renamed the original exe and you can't capture it with find window/get module name, etc. then thats on the user really not a developer problem as such.


Guy Fawkes(Posted 2014) [#8]
Well the reason is because I don't want people complaining about how when they opened a second instance and had alot of 3D data in the scene, that it would wind up lagging then crashing.


Guy Fawkes(Posted 2014) [#9]
@H&K: Good idea. I tried but can't seem to get this function to work correctly.

At the top of your main program, put:

Check_TimeStamp()

in order to create a "Data/" folder & place "Log.txt" inside the folder.

I can't get it to compare right.

TestInstances.bb:



Thank You so kindly! :)

Sincerely,

~GF


LineOf7s(Posted 2014) [#10]
I don't want people complaining about how when they opened a second instance and had alot of 3D data in the scene, that it would wind up lagging then crashing.

They will anyway.


Guy Fawkes(Posted 2014) [#11]
This is NOT your personal thread to hijack. If you're not going to be of any help & continue to whine and complain & try to put me down, then LEAVE this thread & DON'T come back! And I don't care WHAT you have to say after this post.


LineOf7s(Posted 2014) [#12]
Wow.


Kryzon(Posted 2014) [#13]
Thank you so kindly.


wmaass(Posted 2014) [#14]
In all seriousness, there is always the danger of over thinking or over doing it. Implement a simple check and if your users monkey about, well shame on them.


H&K(Posted 2014) [#15]
Bear in mind that I don't have b3D

 	DeleteFile("Data"+"\"+"Log.txt")
		unixtime = GetUnixTimestamp()
		timestamp = WriteFile("Data"+"\"+"Log.txt")
		WriteLine timestamp, unixtime
		CloseFile timestamp
		new_timestamp = OpenFile("Data"+"\"+"Log.txt")
		ntimestamp = ReadLine(new_timestamp)
		If ntimestamp$ <> unixtime Then End
		CloseFile(new_timestamp)
	DeleteFile("Data"+"\"+"Log.txt")


Your code is,
1) Delete file
2) Create file
3) See if file has different value

Should be
1) Does file exist, if not create it, exit check
2) Does file have a very different value (one min say), if so exit program
3) Change file to new value (I personally would just change it rather than delete it every time)


Guy Fawkes(Posted 2014) [#16]
I get steps 1 and 3, but step 2 partially eludes me. How would I check every minute for a different value & should this function be called in the main loop? If so, how?

Thank You so kindly, H&K!

Sincerely,

~GF


RGR(Posted 2014) [#17]
..


Guy Fawkes(Posted 2014) [#18]
Ignoring LineOf7s, Kryzon, & RGR, I found a sneaky way to do it! =D

You take the program's "App Title" string, and compare it with a string of the actual file name, compile the program, then encrypt the compiled program so that the string cannot easily be seen by opening the program in a text editor!




H&K(Posted 2014) [#19]
Step
0) Create the time stamp (TS)
1) As Above
2) Read file, and from that Recreate Time Stamp (RTS)
2b) if TS - rts < 60 then fail. (from your time stamp creation)

If you dont have any pseudo multitasking code, then yes in main loop.

How to do it rarely depends on how you have "main looped", and there are better ways than this one, BUT I know you will have almost defiantly got a "flip" so. And some of these variables you might already by creating

1) Have a variable (probably global), every flip add to it how many milli-secs have passed since last flip. (count=count + (currentmill- oldmilli))
2) If count is greater than 6001 (say) then gosub (lol joke) the check and reset count.

Obviously you have got a time creation function using api_GetSystemTime, and so you could use that instead of whatever millisecond function b3d has (if any)


Guy Fawkes(Posted 2014) [#20]
Oh how I despise you, gosub :P