Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
One down - all dead (NetWork) VFP 3.0
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00231634
Message ID:
00250977
Views:
22
>George,
>
>I have created a program MUTEX:
>DEFINE CLASS "MUTEX" AS FORM
>.placed Single Instance App Code there
>.INIT
>.DISPLAYINSTANCE
>.DESTROY
>ENDDEFINE
>From the Main Startup Program: DO MUTEX
>
>No Errs and No Effect
>
>Still missing a piece? Instantiation occurs when?

Hi Edgar,

Check out the code below. You can simply copy it and use from the post.
DEFINE CLASS Mutex AS CUSTOM
* Author: George Tasker
* Date: February 14, 1998 - 11:41 AM
* Purpose: Creates a mutex used to determine
* whether or not an instance of an 
* application exists.

  hMutex = 0
  nMutexError = 0
  cFormCaption = ""
  
  PROCEDURE Init
    
    LPARAMETERS 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
    * the initial 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)
      * Make sure it's active
      = BringWindowToTop(lnhWnd)
      * Determine if the application
      * is minimized (an icon)
      llicon = (IsIconic(lnhWnd) # 0)
      IF llicon
        * It's an icon, so restore it
        = ShowWindow(lnhWnd, 9)
      ENDIF
    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

* Below is the generic code for to be added to your main program

* Program: MutexMain.prg
* Author: George Tasker
* Date: February 15, 1998 - 10:46 AM
* Purpose: Startup template for
* single instance application

#DEFINE ERROR_ALREADY_EXISTS 183
LOCAL oMutex, lcformcaption
_SCREEN.Visible = .F.
* An SDI form should set 
* lcformcaption equal to
* the displayed form's caption
* which should not be equal to _SCREEN's
lcformcaption = _SCREEN.Caption
_SCREEN.Caption = ""
SET PROCEDURE TO Mutex ADDITUVE
oMutex = CREATEOBJECT('Mutex', lcformcaption)
IF oMutex.nMutexErr # ERROR_ALREADY_EXISTS
  * The instance doesn't exist and the
  * remaining set up code goes here
  READ EVENTS
ELSE
  * It does, so display the 
  * orginal before terminating
  oMutex.DisplayInstance()
ENDIF
oMutex = .NULL.
The only problem that you might have is if you don't name the individual procedure file Mutex.prg.

hth.
George

Ubi caritas et amor, deus ibi est
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform