B4J Question B4JPackager11 and Inno - How do I restrict to one instance running on Windows?

gregchao

Active Member
Licensed User
Longtime User
I have successfully used the B4jPackager11/Inno process to create an install .exe for my application. Is there a way to set a restriction so that only one instance of my application can run? I saw some old posts using lauchb4j but I assume that it does not apply to the newer B4jPackager11/Inno process. Any help would be appreciated.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4JPackager11 / inno setup will not help you with this.

You need to do it in your code.
Example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private AppIsRunning As Boolean = True
    Private const LOCK_FILE As String = "lock"
End Sub

Public Sub Initialize
   
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xui.SetDataFolder("my app") 'change to make sure that it is unique.
    If IsAppLocked Then
        Dim sf As Object = xui.MsgboxAsync("App is already running", "")
        Wait For (sf) Msgbox_Result (Result As Int)
        ExitApplication
    Else
        AppIsRunning = True
        UpdateLockFile
    End If
End Sub

Private Sub B4XPage_Background
    If AppIsRunning Then
        AppIsRunning = False
        File.Delete(xui.DefaultFolder, LOCK_FILE)
    End If
End Sub

Private Sub IsAppLocked As Boolean
    If File.Exists(xui.DefaultFolder, LOCK_FILE) = False Then Return False
    Try
        Dim ticks As Long = File.ReadString(xui.DefaultFolder, LOCK_FILE)
        If ticks + 30 * DateTime.TicksPerSecond > DateTime.Now Then
            Return True
        End If
    Catch
        Log(LastException)
    End Try
    Return False
End Sub


Private Sub UpdateLockFile
    Do While AppIsRunning
        Try
            File.WriteString(xui.DefaultFolder, LOCK_FILE, DateTime.Now)
        Catch
            Log(LastException)
        End Try
        Sleep(10 * DateTime.TicksPerSecond)
    Loop
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…