Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Two or more instances of my application
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01685332
Message ID:
01685334
Views:
53
>Friends; How to avoid opening two or more instances of my application at the same time?

There are several ways to do it --- I got this from a post here a long time ago:

* test for existence of Windows mutex for this application window
#DEFINE ERROR_ALREADY_EXISTS 183
PRIVATE oMutex
_SCREEN.VISIBLE = .F.
*_SCREEN.CAPTION = ""
oMutex = CREATEOBJECT('Mutex', u_AppName)
IF oMutex.nMutexErr = ERROR_ALREADY_EXISTS
* Mutex exists so display the original instance before terminating this one.
oMutex.DisplayInstance()
RETURN && say goodbye, cause cleanup, object falls out of scope, destroy
ELSE
* The instance doesn't exist, program is not already running, start it
_SCREEN.VISIBLE = .T.
ENDIF

************************************************************
DEFINE CLASS Mutex AS CUSTOM
************************************************************
cformcaption = ""
hmutex = 0
nMutexErr = 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)
.nMutexErr = GetLastError()
ENDWITH
RETURN
ENDPROC
PROCEDURE DisplayInstance
#DEFINE SW_SHOWNORMAL 1
#DEFINE SW_SHOW 5
DECLARE INTEGER FindWindow IN Win32API STRING @lpClassName, STRING @lpWindowName
DECLARE SHORT IsIconic IN Win32API INTEGER HWND
DECLARE LONG SHOWWINDOW IN Win32API LONG HWND, LONG 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 application is minimized
IF IsIconic(lnhwnd) # 0
= SHOWWINDOW(lnhwnd, SW_SHOWNORMAL )
ENDIF
= BringWindowToTop(lnhwnd)
ELSE
* This handles applications with dynamic title text bars.
= MESSAGEBOX("Application "+lcformcaption+" is already running.", ;
64, "Only One Instance Allowed")
ENDIF
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
*RELEASE LIBRARY ALL
*RETURN
ENDPROC
ENDDEFINE
ICQ 10556 (ya), 254117
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform