Tim,
I used the windows api and implemented it as a class.
I hope this is what you need.
Bob Lee
#DEFINE ERROR_ALREADY_EXISTS 183
LOCAL oMutex, lcFormCaption
lcFormCaption = "SE New GoldMine Record Ver 8.00"
oMutex = CREATEOBJECT("Mutex", lcFormCaption)
IF oMutex.nMutexError # ERROR_ALREADY_EXISTS
* The instance doesn't exist
DO form newrec7 NAME ofrmnewrec linked
READ events
ELSE
* It does exist so display the
* original instance before terminating
oMutex.DisplayInstance()
ENDIF
oMutex = .NULL.
DEFINE CLASS Mutex AS CUSTOM
cFormCaption = ""
hMutex = 0
nMutexError = 0
PROCEDURE Init
LPARAMETER pcFormCaption
DECLARE INTEGER CreateMutex IN Win32API;
STRING @lpMutexAttributes,;
STRING bInitialOwner,;
STRING @lpName
DECLARE INTEGER GetLastError IN Win32API
LOCAL lcInitOwner, lcName, lcFormCaption
* So that the Mutex name is different
* from the caption.
lcFormCaption = pcFormCaption
lcName = CHRTRAN(pcFormCaption, SPACE(1), "")
* Simulate a BOOL type equaling TRUE.
lcInitOwner = CHR(1)
WITH THIS
.cFormCaption = lcFormCaption
.hMutex = CreateMutex(0, lcInitOwner, @lcName)
.nMutexError = GetLastError()
ENDWITH
RETURN
ENDPROC
PROCEDURE DisplayInstance
* This procedure is used to display
* an instance of the application.
DECLARE INTEGER FindWindow IN Win32API ;
STRING @lpClassName, STRING @lpWindowName
DECLARE SHORT IsIconic IN Win32API ;
INTEGER hWnd
DECLARE INTEGER ShowWindow IN Win32API ;
INTEGER hWnd, INTEGER nCmdShow
DECLARE SHORT BringWindowToTop IN Win32API ;
INTEGER hWnd
LOCAL lcFormCaption, lnhWnd, llIcon
lcFormCaption = THIS.cFormCaption
lnhWnd = FindWindow(0, @lcFormCaption)
IF NOT EMPTY(lnhWnd)
* Determine if the Application
* is minimized (an icon).
llIcon = (IsIconic(lnhWnd) # 0)
IF llIcon
* It is an icon, so restore it
= ShowWindow(lnhWnd, 9)
ENDIF
* Make sure it is active.
= BringWindowToTop(lnhWnd)
ELSE
* This handles applications with
* dynamic title text bars.
= MESSAGEBOX("You already have an instance" +;
" of this application running." + CHR(13) +;
CHR(13) + "Please check your task bar", ;
64, "Only one instance allowed.")
ENDIF
RETURN
ENDPROC
PROCEDURE Destroy
DECLARE INTEGER WaitForSingleObject IN Win32API ;
INTEGER hObject, INTEGER dwTimeOut
DECLARE SHORT ReleaseMutex IN Win32API ;
INTEGER hMutex
DECLARE SHORT CloseHandle IN Win32API ;
INTEGER hObject
LOCAL lnResult
IF THIS.hMutex # 0
lnResult = WaitForSingleObject(THIS.hMutex, 50)
* 258 = STATUS_TIMEOUT
IF lnResult # 258
lnResult = ReleaseMutex(THIS.hMutex)
* 1 equals boolean true
IF lnResult = 1
lnResult = CloseHandle(THIS.hMutex)
ENDIF
ENDIF
ENDIF
CLEAR DLLS
RETURN
ENDPROC
ENDDEFINE
Previous
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only