Advertise Information
Trapped in somebody else's frames?  BreakOut!!!

Detecting and activating a previous instance of your application

This routine will prevent two copies of your program from running at the same time. It consists of a Function that determines if another instance is already running and activates it if it is. The Sub (Form_Load()) calls this function and closes the program if there is another instance of the program running.

Function AnotherInstance () As Integer
    Dim AppTitle$
    If App.PrevInstance Then
        ' Hold the title of the application (title bar caption)
        AppTitle$ = App.Title  
        ' Change our application title
        App.Title = "No longer want this app running..." 
        ' Activate the previous instance
        AppActivate AppTitle$  
        ' Let calling procedure know another instance was detected
        AnotherInstance = True  
    Else
        ' Let calling procedure know another instance was NOT detected
        AnotherInstance = False 
    End If
End Function

Sub Form_Load ()
    ' Don't want two copies of the program running at the same time
    If AnotherInstance() Then End 
    ' Note: that this routine will not work if the application's title changes 
    ' (showing file names in the title bar for example).
End Sub