-
1
- #1
In the old days, semaphores were created for all kinds of stuff and were usually implemented by the creation of zero byte text files - if the file existed, then do this, else do that. Then, object-orientated programming came along and semaphore usage rode off into the sunset...kinda...
There are still cases where semaphores are usefull and can be easily implemented using a few Windows APIs. For example, have you ever had an EXE that you wanted to force only one instance - make it so that your customers could not create more than one instance? Here is how you do it. Put the following code at the very top of your main.prg (or whatever):
There are still cases where semaphores are usefull and can be easily implemented using a few Windows APIs. For example, have you ever had an EXE that you wanted to force only one instance - make it so that your customers could not create more than one instance? Here is how you do it. Put the following code at the very top of your main.prg (or whatever):
Code:
IF _VFP.StartMode = 4
LOCAL lcSmp, hSmp
lcSmp = "The name of your EXE without the extension"
#DEFINE STANDARD_RIGHTS_REQUIRED 0xf0000
DECLARE INTEGER CreateSemaphore IN kernel32.dll ;
INTEGER lcSmAttr, INTEGER lInitialCount, INTEGER lMaximumCount, STRING lpName
DECLARE INTEGER OpenSemaphore IN kernel32.dll ;
INTEGER dwDesiredAccess, INTEGER bInheritHandle, STRING lpName
hSmp = OpenSemaphore(STANDARD_RIGHTS_REQUIRED, 0, lcSmp)
IF hSmp = 0
hSmp = CreateSemaphore(0, 1, 1, lcSmp)
IF hSmp = 0
RETURN
ENDIF
ELSE
RETURN
ENDIF
ENDIF