B4J Question Prohibits the second instance of the program from running

vmag

Active Member
Hi, everybody. As you know, on the 64 platform after the Package mode, we have an executable exe file, on the 32 platform we get an executable jar file. Is there a way to prevent these files from running in more than one instance? For example, the user started the program, minimized it to the tray, and forgot it. Then it runs the same program again... The best option is when, instead of restarting the program, the previously minimized program will be activated from the tray, or at least the re-launched program will display a message about restarting and shut down.
 

vmag

Active Member
Thank you, but this is more suitable for controlling the launch of other applications than your own. In OS 64, it is an OpenJDK process... in OS 32, it is a Java(TM)... process and it says little and there is no universality. You can try to create and hold a local text file at startup, for example. the application can start if there is no file or it is there, but you can delete it, create a new one and keep it open
 
Upvote 0

vmag

Active Member
I can't get exclusive access to the file
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Log(File.DirApp)
    If File.Exists(File.DirApp, "flag1.txt") = True Then
        Try
            File.OpenOutput(File.DirApp, "flag1.txt",True)
        Catch
            xui.MsgboxAsync("The app is already running!", "Attention!")
            ExitApplication
        End Try
    End If
End Sub
 
Upvote 0

vmag

Active Member
This is how mono app launch works, but I had to use a timer
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Timer1 As Timer
End Sub


Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Timer1.Initialize("Timer1_Tick", 500)
    If File.Exists(File.DirApp, "flag1.txt") = True Then
        File.WriteString(File.DirApp, "flag1.txt","")
        Sleep(1000)
        If File.ReadString(File.DirApp, "flag1.txt").Length <>0 Then
            xui.MsgboxAsync("The program was launched earlier!", "Attention !")
            Sleep(2000)
            ExitApplication
            Else
            Timer1.Enabled = True
        End If
        Else
        xui.MsgboxAsync("File flag1.txt not found!", "START")
    End If
End Sub

Private Sub Timer1_tick_tick
    File.WriteString(File.DirApp, "flag1.txt","bla... bla... bla...")
End Sub
the text file must be present in the project
 
Upvote 0
Top