Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to detect app already runs and kill other instance
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Problèmes
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01200641
Message ID:
01200650
Vues:
22
There are many ways to check running instance of the application.
One of them is using Mutex.
For example (this is not my original code - I don't remember the author):
Function udf_IsAppAlreadyRunning()

   #Define ERROR_ALREADY_EXISTS  183
   #Define ERROR_ACCESS_DENIED     5
   #Define SW_NORMAL               1
   #Define SW_MAXIMIZE             3
   #Define SW_RESTORE              9
   #Define SW_SHOWDEFAULT         10
   #Define GW_HWNDNEXT             2
   #Define GW_CHILD                5

   Local lnhMutex, lnHwnd, llReturn,lnErrNum
   * You can define the next two variables in the header file
   Local lcGUIDForMutex, lcGUIDForScreen,
   lcGUIDForMutex  ="JS_4C45A74D"
   lcGUIDForScreen ="JS_5D1BD013"

   lnhMutex = CreateMutex(0, 1, lcGUIDForMutex)
   lnErrNum = GetLastError()
   If lnErrNum= ERROR_ALREADY_EXISTS OR lnErrNum = ERROR_ACCESS_DENIED
      CloseHandle(lnhMutex)
      llReturn = .T.
      lnHwnd = GetWindow(GetDesktopWindow(), GW_CHILD)
      Do While lnHwnd != 0 && loop through all windows
         If GetProp(lnHwnd, lcGUIDForScreen) = 1
            If IsIconic(lnHwnd)
               ShowWindowsVFP(lnHwnd,SW_RESTORE)
            Endif
            BringWindowToTop(lnHwnd)
            ShowWindowsVFP(lnHwnd,SW_NORMAL)
            llReturn = .T.
            Exit
         Endif
         lnHwnd = GetWindow(lnHwnd, GW_HWNDNEXT)
      Enddo
      CloseHandle(lnHwnd)
      CloseHandle(lnhMutex)
      Clear Dlls "BringWindowToTop", "GetDesktopWindow", ;
         "GetProp", "GetWindow", "ShowWindow", ;
         "CloseHandle"
   Else
      =SetProp(_vfp.HWnd, lcGUIDForScreen, 1)
      _Screen.AddProperty("MutexHandle",lnhMutex)
      llReturn = .F.
   Endif
   Clear Dlls "CreateMutex", "SetProp"
   Return (llReturn)
Endfunc


********************************************************************************
* Procedure udp_DeclareCommonDLL
* Purpose: declaring WinAPI functions for use in application
********************************************************************************
Procedure udp_DeclareCommonDLL
   *****************************************************************************
   * Declaring common functions
   *****************************************************************************
   Declare Integer GetPrivateProfileString In Win32API ;
      As GetPrivStr ;
      String cSection, String cKey, String cDefault, String @cBuffer, ;
      Integer nBufferSize, String posfile
   Declare Integer WritePrivateProfileString In Win32API ;
      As WritePrivStr ;
      String cSection, String cKey, String cValue, String posfile
   Declare Integer GetDesktopWindow In User32
   Declare Integer GetWindow In User32 Integer HWnd, Integer uCmd
   Declare Integer BringWindowToTop In Win32API Integer HWnd
   Declare Integer IsIconic In user32 Integer HWnd
   Declare Integer ShowWindow In Win32API As ShowWindowsVFP ;
      Integer HWnd, Integer nCmdShow
   Declare Integer GetProp In User32 Integer HWnd, String  lpString
   Declare Integer SetProp In User32 ;
      Integer HWnd, String lpString, Integer hData
   Declare Integer GetLastError In Win32API
   Declare Integer CloseHandle In Kernel32 Integer hObject
   Declare Integer CoCreateGuid In ole32 String @pguid
   Declare Integer StringFromGUID2 In ole32;
      STRING rguid, String @lpsz, Integer cchMax
   *****************************************************************************
   * Declaring Mutex functions
   *****************************************************************************
   Declare Integer CreateMutex  In Win32API ;
      Integer lpMutexAttributes, Integer bInitialOwner, String lpName
   Declare Integer ReleaseMutex In Win32API Integer hMutex
Endproc
But I never use the killing process of the oldest instance.
Hope this help.

WBR,
Juri


>Hello everybody,
>
>Sometimes our app is seen to be still existing (when opening the task manager).
>
>- How could I detect it is already running and..
>- How can I kill the process of the oldest instance by a call from the newer instance?
>
>TIA,
>
>Lennert
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform