Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Detect Copy of itself running
Message
From
24/04/1998 10:59:52
 
 
To
24/04/1998 08:54:49
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00093966
Message ID:
00094675
Views:
24
Okay, here's the strategy: You put a dummy file in the data directory. Then use low-level file calls to lock it completely or to open it in a sharing mode. When you want to get exclusive access to the data, you lock the file completely. If anyone else is in the system, they will already be using the file so you won't be able to do it and you can give an error. Well, whatever, let me just send up some code.

Alright: this is a little cheesy because it's hardcoding my system filename. But you can modify it slightly. Also check out the 2 public variables that I assume you have already declared when you call these functions: g_cDataPath is your data directory (put a backslash at the end.) and g_nSysHandle should be 0 or negative something.

Advantages in using this strategy: 1. the network file locking routine handles all your locking, which is pretty safe. 2. It doesn't so much as touch your real data to determine whether it can get in or not.

If you like the strategy you can clean it up and put it in the files section here.

-Adam



* ve_excl Library determining whether we
* have exclusive use of the data or not by Adam Weisberg
* Note global variables: g_cDataPath is the dir of you data
* which needs to end in a backslash
* Public g_nSysHandle should be set to 0 or -1 before you run this
* in your main calling program.

*******************
Function EnterShare
*******************
* This should be run on entry to the system
**********************************************************
* It returns:
* 1 if we can enter the system (multiuser)
* 2 if we cannot enter the system as it is being used excl
* 3 if we got an error.
**********************************************************
Local l_nHandle
* Close the system file if it is open.
IF TYPE('g_nSysHandle') = 'N' AND g_nSysHandle > 0
=FCLOSE(g_nSysHandle) && Close the system file
ENDIF
IF !File(g_cDataPath + 'MC001.SYS')
l_nHandle=FCreate(g_cDataPath + 'MC001.SYS')
=FPuts(l_nHandle,Chr(26) + 'ANW 1998')
=FCLOSE(l_nHandle)
ENDIF
IF !File(g_cDataPath + 'MC001.SYS')
=MessageBox('Error creating system file.'+Chr(13)+;
'Make sure you have Write access to the data directory.' + Chr(13) +;
'(This has to do with your network setup.)',16,'Cannot Enter System')
RETURN 3
ENDIF
g_nSysHandle = FOPEN(g_cDataPath + 'MC001.SYS')
IF g_nSysHandle < 0
RETURN 2
ELSE
RETURN 1
ENDIF
ENDPROC

******************
Function EnterExcl
******************
* Returns
* 1 If I can get exclusive use
* 2 If I can't
* 3 If there is an error that means I need to be booted
Local l_nHandle
* Close the system file if it is open.
IF TYPE('g_nSysHandle') = 'N' AND g_nSysHandle > 0
=FCLOSE(g_nSysHandle) && Close the system file
ENDIF
* Create the system file if it does not exist.
IF !File(g_cDataPath + 'MC001.SYS')
l_nHandle=FCreate(g_cDataPath + 'MC001.SYS')
=FPuts(l_nHandle,Chr(26) + 'ANW 1998')
=FCLOSE(l_nHandle)
ENDIF
IF !File(g_cDataPath + 'MC001.SYS')
=MessageBox('Error creating system file.'+Chr(13)+;
'Make sure you have Write access to the data directory.' + Chr(13) +;
'(This has to do with your network setup.)',16,'Cannot Enter System')
RETURN 3
ENDIF
g_nSysHandle = FOPEN(g_cDataPath + 'MC001.SYS',12) && Read/Write/Unbuffered
IF g_nSysHandle < 0 && Failure - try to reopen SHARED
g_nSysHandle = FOPEN(g_cDataPath + 'MC001.SYS',0)
IF g_nSysHandle < 0
RETURN 3
ELSE
RETURN 2
ENDIF
ELSE
RETURN 1
ENDIF
ENDPROC



***************
Function OnExit
***************
=FCLOSE(g_nSysHandle)
ENDPROC
Previous
Reply
Map
View

Click here to load this message in the networking platform