Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single Instance Of App Using Only A Table 1

Status
Not open for further replies.

torturedmind

Programmer
Jan 31, 2002
1,052
PH
Hi all,

It's been a while since I visited here. Anyways, I just wanted to share this piece of code that I use to check the single instance of my application. I already saw all "one-instance-of-app" in the FAQ area and they've all been proven to be of great use. But this time, I tried to use stuffs native to VFP - the table and the TRY...CATCH... block. I sure hope this will help anyone that might need it. Note that this code may need to be modified to fit your needs.

Code:
[COLOR=#4E9A06]* This code should be placed in the MAIN.PRG.
* The key is the table [b]req_id.sys[/b] being created
* as soon as the program starts then USE it EXCLUSIVEly.
* [b]req_id.sys[/b] (the table) could be any name you desire.[/color]

[COLOR=#4E9A06]***** Check single instance of app, my way *****
* See if table already exist[/color]
IF FILE(ADDBS(SYS(2023)) + "req_id.sys")
[COLOR=#4E9A06]        * If table is already there, it was probably created by the previous instance of the app
        * but there might have been a crash or something that the app failed to delete it prior 
        * to a garceful exit[/color]
	TRY 
                [COLOR=#4E9A06]* TRY to USE it EXCLUSIVEly so that succeeding program loads will not succeed in USEing it[/color]
		USE (ADDBS(SYS(2023)) + "req_id.sys") EXCLUSIVE IN 0
	CATCH TO oUseErr
                [COLOR=#4E9A06]* Check error number 1705 (aka "File access denied" error) which means
                * another instance is already USEing the table[/color]
		IF oUseErr.ERRORNO = 1705
			MESSAGEBOX("Another instance of this application is already running.", ;
				64, "System message")
                        [COLOR=#4E9A06]* Do whatever you need to do here then...[/color]
			QUIT
		ENDIF
	ENDTRY
ELSE
        [COLOR=#4E9A06]* If table does not exist, create it with a different extension having only one field
        * This automatically opens it in EXCLUSIVE mode[/color]
	CREATE TABLE (ADDBS(SYS(2023)) + "req_id.sys") ;
		(prog_id C(10))
        [COLOR=#4E9A06]* Just put any value in prog_id, it really doesn't matter[/color]
	INSERT INTO req_id (prog_id) VALUES (SYS(2015))
ENDIF
************************************************

Before the program quits, discard the table.

Code:
USE IN req_id
ERASE (ADDBS(SYS(2023)) + "req_id.sys")


kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Locking a file is a known solution, yes.

As SYS(2023) is temp and that is a user profile subfolder in most windows versions (xp and later) this actually doesn't prevent a second user logged in to the same computer to start the same application. Which can be fine or not.

Thanks anyway, it's shorter than some of the solutions already known from the FAQ here or from fox wiki or elsewhere, but each has it's own advantages or disadvantages. Eg there are several file unlocker tools to unlock files and then a user knowing which file to unlock can also start a second instance.

You could shorten the CATCH a bit:
Code:
	CATCH TO oUseErr WHEN oUseErr.ERRORNO = 1705
                * Check error number 1705 (aka "File access denied" error) which means
                * another instance is already USEing the table
		MESSAGEBOX("Another instance of this application is already running.", ;
			64, "System message")
                * Do whatever you need to do here then...
		QUIT

Bye, Olaf.
 
Hi Kilroy,

Haven't see you here for ages.

Thanks for posting this. You might consider publishing it as an FAQ here on Tek Tips.

A variation that I've used is to limit the number of users who are allowed to log into the application at one time (not all from the same workstation, of course). You might want to do that for licensing reasons, for example.

You can't do it simply be incrementing a counter when one a user logs in, and decrementing it when they log out, because the counter won't be decremented if a user crashes (an issue which your own code correctly addresses). Instead, you create a shared table with N records, where N is the number of permitted users. As each user logs in, you lock the next available record, and unlock when they exit (or crash). When there are no more records to be locked, you know you've reached the maximum.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks for all your comments and suggestions.

Hi Olaf,
OlafDoschke said:
As SYS(2023) is temp and that is a user profile subfolder in most windows versions (xp and later) this actually doesn't prevent a second user logged in to the same computer to start the same application. Which can be fine or not.
This is ok because we allow multi-user on a single pc.

OlafDoschke said:
Eg there are several file unlocker tools to unlock files and then a user knowing which file to unlock can also start a second instance.
That is possible. But we thought the user will not waste their time acquiring file unlockers just to run a second instance of our app. Nevertheless, we will consider this and probably implement a different approach for such scenario. And thanks for revising the TRY...CATCH statement to a shorter one. [thumbsup2]

Hi Mike,
Yeah, been busy with other (non-computer-related) things. I was trying to brush up my guitar-playing skills (again). Anyways, your own variation is simple yet a clever one. I'll try to use that if the need arises. Thanks.


kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Sorry, I forgot. Thanks for the star.

kilroy [knight]
philippines

"Once a king, always a king. But being a knight is more than enough."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top