Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to prevent the application running twice ?
Message
De
14/08/2007 03:07:07
 
 
À
13/08/2007 23:47:52
Yim Ming Sun Derek
Spacious Design Consultant
Hong Kong, Hong Kong
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01232152
Message ID:
01247974
Vues:
66
>Dear Tore,
>Thank for your reply, but I want to ask what is
>the 'Mutex', I copy your code to my startup program,
>but I got an error for the class 'Mutex' not define.
>
>Please advise.
>
>Thanks.

I'm sorry, here it is.
************************************************************
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
By the way, this is not my code, I found it in an earlier message, message#1101025.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform