-
1
- #1
torturedmind
Programmer
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.
Before the program quits, discard the table.
kilroy
philippines
"Once a king, always a king. But being a knight is more than enough."
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
philippines
"Once a king, always a king. But being a knight is more than enough."