Visual Basic – How can I allow only one instance of my application to run at a time?

--> (Word) --> (PDF) --> (Epub)
This article has been published [fromdate]

You can read the PrevInstance property of the App object, if the value of this is True then another instance of the application is already running. If your program starts with Sub Main, you can use this code to exit the program if another copy is already running:

VB Code:

    1. 'in sub main...

    2. If App.PrevInstance = True Then

    3. MsgBox "Already running...."

    4. Exit Sub

    5. End If

    For forms you need an extra piece of code to also close the form, the following code should be placed in Form_load:

    VB Code:

    1. 'in form_load...

    2. If App.PrevInstance = True Then

    3. MsgBox "Already running...."

    4. Unload Me

    5. Exit Sub

    6. End If

    SOURCE

    LINK (Vbforums.com)

    LANGUAGE
    ENGLISH