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