How to stop my ABC application from being run twice

I would like to prevent the user from launching a second copy of my Clarion app. So far the instruction “don’t click on the shortcut icon multiple times” hasn’t been particularly effective. :grin:

In VB.NET I can select “Make Single instance application” and there is an option to only load one instance of the Clarion IDE, but what about only one copy of the application?

In VB6 I have the following code to detect multiple instances of my Zippy.exe application:

Sub ActivatePrevInstance()
'// Activate the previous instance of the application
Dim OldTitle As String
Dim PrevHndl As Long
Dim result As Long

    'Save the title of the application.
    OldTitle = App.Title

    'Rename the title of this application so FindWindow
    'will not find this application instance.
    App.Title = "Zippy!"

    'Attempt to get window handle using VB class name.
    PrevHndl = FindWindow(vbNullString, OldTitle)

    'Check if found
    If PrevHndl = 0 Then ' IF 1
        'No previous instance found.
        App.Title = OldTitle
        Exit Sub
    End If ' IF 1

    'Get handle to previous window.
    PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)

    'Restore the program.
    result = OpenIcon(PrevHndl)

    'Activate the application.
    result = SetForegroundWindow(PrevHndl)

    End
End Sub 'ActivatePrevInstance

I’m sure someone has written something similar for Clarion. It may even be in one of the many examples, but I’m having difficulty finding it. Any suggestions?

Hi Donn

look at BeginUnique (Set Application to Run in a Single Process) in the help

Best regards,
Eric

3 Likes

Donn,

There is Frame extension which you can add to Main procedure, it has “Run only one instance of the app” option.

4 Likes

Thank you both for your suggestions. I had trouble figuring out where to put the BeginUnique code, so I have used the FrameExtension and it is doing exactly what I need.

Donn,

If you don’t have a Frame, you can use BeginUnique like this:

Blockquote

                PROGRAM

                MAP
                    INCLUDE('EQUATES.CLW'),ONCE 
                    INCLUDE('CWUTIL.INC'),ONCE

ProcBeginUnique PROCEDURE
END

GLO:ApplicationName EQUATE(‘MyApplicationName’)
UseBeginUnique LONG

MyWindow WINDOW(‘UseBeginUnique’),AT(,240,100),GRAY,FONT(‘MS Sans Serif’,10, |
FONT:regular)
BUTTON(’&Exit’),AT(102,74,36,14),USE(?ExitButton)
END

CODE

    OPEN(MyWindow)
    ACCEPT
        CASE FIELD()
        OF 0
            CASE EVENT()
            OF EVENT:OpenWindow
                UseBeginUnique = BeginUnique(GLO:ApplicationName)
                IF ~UseBeginUnique
                    MESSAGE('This Application is already running!','Info',ICON:Exclamation)
                    RETURN
                END
                ProcBeginUnique
                EndUnique(GLO:ApplicationName)
            END
        OF ?ExitButton
            CASE EVENT()
            OF EVENT:Accepted
                POST(EVENT:CloseWindow)
            END
        END
    END

ProcBeginUnique PROCEDURE

CODE

    MESSAGE('After closing this message, try to restart|a new instance of this application!','Info',ICON:Asterisk)

Blockquote

3 Likes

Dear Eric
Fortunately my app has a frame, but I will definitely keep this code in my bag of tricks for future use.
Thank you once again.
Donn

You’re welcome Donn.

1 Like