1

Konu: Already open

Hello:

I my *main* program, I have ...


ON ERROR DO AlreadyOpen
---
---
PROCEDURE AlreadyOpen
    MESSAGEBOX('This application is already open. Click "OK" to close this window!',0,'ERROR MESSAGE')
    CLEAR PROGRAM
    CLOSE ALL
    CLEAR ALL
    QUIT
ENDIF

... in order to prevent opening a second version of an application and getting a VPF9 error message. But it does not work (a lot of problems). How do you really prevent  a second version of an app to open?

Regards,

Hans L

2

Re: Already open

Hi Hans L you could try this below codes

Visual Fox Pro
=myInstance("A name for your Application")

 
Then Just Copy the following Code And Add it At the End Of your Main.prg.
 
******************************************
Procedure myInstance
    Parameters myApp
    =Ddesetoption("SAFETY",.F.)
    ichannel = Ddeinitiate(myApp,"ZOOM")
    If ichannel =>0
        =Ddeterminate(ichannel)
        Quit
    Endif
    =Ddesetservice(myApp,"define")
    =Ddesetservice(myApp,"execute")
    =Ddesettopic(myApp,"","ddezoom")
    Return
Endproc
******************************************
Procedure ddezoom
    Parameter  ichannel,saction,sitem,sdata,sformat,istatus
    Zoom Window Screen Norm
    Return
Endproc
**********************************************************

and another solution for this goal

Visual Fox Pro
Function IsRunning(tcSemaphoreName) && your exe

    Local lpszSemName
    #Define ERROR_ALREADY_EXISTS 183
    Declare Integer GetLastError In win32API
    Declare Integer CreateSemaphore In WIN32API ;
        STRING @ lpSemaphoreAttributes, ;
        LONG lInitialCount, ;
        LONG lMaximumCount, ;
        STRING @ lpName
 
    hsem = CreateSemaphore(0,0,1,tcSemaphoreName)
 
    Return (multirun # 0 .And. GetLastError() == ERROR_ALREADY_EXISTS)

3 Son düzenleyen, cetinbasoz (11.02.2011 12:09:21)

Re: Already open

Hans,
This is what I am using:

Visual Fox Pro
*Main.prg

 
#define APPTITLE "My Application"
 
* enter a unique name - whatever value you prefer ie: HansL_AccountingApp
* it may also be a GUID value generated once for each of your applications.
* I generated one for you if you want to use: "49E83EC0-C2DD-4823-A5E7-DAF8CA0005F6"
 
lcAppName = "AUniqueNameForYourApplication"
If IsAppRunning(m.lcAppName) And ;
        Messagebox('Another instance of this software is already running.'+Chr(13)+;
        'Do you want to run another one?'+Chr(13)+;
        'Yes - Run another instance'+Chr(13)+;
        'No - Exit',4+32+256+4096,APPTITLE,30000) = 7
    Return && Do nothing - quit to disable running more than one instance
ENDIF
 
* ... Other code it is OK to proceed
 
 
Function IsAppRunning
    Lparameters tcSemaphoreName
    Local hsem, lpszSemName
    #Define ERROR_ALREADY_EXISTS 183
    Declare Integer GetLastError In win32API
    Declare Integer CreateSemaphore In WIN32API ;
        string @ lpSemaphoreAttributes, ;
        LONG lInitialCount, ;
        LONG lMaximumCount, ;
        string @ lpName
    hsem = CreateSemaphore(0,0,1,m.tcSemaphoreName)
    Return (m.hsem # 0 And GetLastError() == ERROR_ALREADY_EXISTS)
endfunc

4

Re: Already open

Thank you, guys. I will try to decipher all three code sections (in order to learn!!!), and select the one I understand best :-)

Best regards,

Hans L

5

Re: Already open

I assume that the codes above work only for an *app* an not when you run the main file in the Program Manager? At least, Icould not get it to work. While I would appreciate comments on this, I have put this on the backburner for now (but I will use it later, when I am closer to *building* an app).

Regards,

Hans L

6

Re: Already open

It does work from an exe or from within VFP IDE (I think that is what you meant). To try, after running it there launch another instance of VFP and try to run main from there too (or run the exe).

7

Re: Already open

Will try again. I did not get the code to work, so ... Will report back in a while.

Regards,

Hans L

8

Re: Already open

cetinbasoz yazdı:

Hans,
This is what I am using:

Visual Fox Pro
*Main.prg

 
#define APPTITLE "My Application"
 
* enter a unique name - whatever value you prefer ie: HansL_AccountingApp
* it may also be a GUID value generated once for each of your applications.
* I generated one for you if you want to use: "49E83EC0-C2DD-4823-A5E7-DAF8CA0005F6"
 
lcAppName = "AUniqueNameForYourApplication"
If IsAppRunning(m.lcAppName) And ;
        Messagebox('Another instance of this software is already running.'+Chr(13)+;
        'Do you want to run another one?'+Chr(13)+;
        'Yes - Run another instance'+Chr(13)+;
        'No - Exit',4+32+256+4096,APPTITLE,30000) = 7
    Return && Do nothing - quit to disable running more than one instance
ENDIF
 
* ... Other code it is OK to proceed
 
 
Function IsAppRunning
    Lparameters tcSemaphoreName
    Local hsem, lpszSemName
    #Define ERROR_ALREADY_EXISTS 183
    Declare Integer GetLastError In win32API
    Declare Integer CreateSemaphore In WIN32API ;
        string @ lpSemaphoreAttributes, ;
        LONG lInitialCount, ;
        LONG lMaximumCount, ;
        string @ lpName
    hsem = CreateSemaphore(0,0,1,m.tcSemaphoreName)
    Return (m.hsem # 0 And GetLastError() == ERROR_ALREADY_EXISTS)
endfunc

I am back on this. I am trying the code above first, but when I place it at the beginning of the Main.prg file, it does not work.

But, before I ask questions, please let me know where, in relation to the code below in my Main.prg file I should place the code above provided by Cetin?

Visual Fox Pro
*   code preparing 

 
DO Mame
READ EVENTS
 
*   code closing down

Thanks/Hans L

9

Re: Already open

You can place the function at the end of the main.prg (or before any class definitions that may happen to be there). Then place the code part at top of main.prg. It works for me like that.