MakeSingleInstance subroutine.

Started by Quin, February 23, 2025, 04:32:18 AM

Previous topic - Next topic

Quin

Quote from: MrBcx on February 23, 2025, 07:50:31 AM
That's handy but you're reinventing wheels again  ;)


https://bcxbasiccoders.com/webhelp/html/findfirstinstancefunction.htm
Oh wow, no idea how I missed this one! Thanks, this is even better than my implementation :)

MrBcx


Quin

I wrote this super simple subroutine to only allow users to run one instance of my app at a time. If they try to run another, the app will display an error dialog and not let them do so.

Sub MakeSingleInstance(AppID$)
    Local As HANDLE hMutex
    hMutex = OpenMutex(MUTEX_ALL_ACCESS, 0, AppID$ + "_IsAlreadyRunning")
    If Not hMutex Then
        hMutex = CreateMutex(0, 0, AppID$ + "_IsAlreadyRunning")
    Else
        Msgbox "Another instance of " + AppID$ + " is already running.", "Error", MB_ICONERROR
        End
    End If
End Sub

Enjoy  ;)