Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to test if Application is already running?
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01101016
Message ID:
01101025
Vues:
29
>How can you test if your VFP application is already running? I have been
>using a function called "OnceOnly" that goes out and checks if the Window
>titles running match the one you want to load and if it does, it stops the
>loading of the app and sets focus to the running app. This works most of
>the time, but now my client wants me to change the Window Title of the
>application with the name of the user after the title. The "OnceOnly"
>function now does not see the application as running and starts another
>one. Does anyone know of another way to restrict the same application from
>running more than once at a time?
* 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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform