Share My Creation B4J single instance

I wanted to limit my app to running as a single instance, in both Windows and Linux.

Several solutions for this single instance problem have been posted, but usually do not include running in Linux.

Erel made a few suggestions in one of his posts. One of these was to use an external file as a marker to indicate that the app is already running.

I took on the task of doing this, and here is my solution.

The "single_instance_test" example attached has many comments which describe how the app works.
 

Attachments

  • single_instance.zip
    334.6 KB · Views: 567

rraswisak

Active Member
Licensed User
Longtime User
Hi,

This trick is work if you execute same jar file at the same path, if you run the second jar from different path then the trick does not work.

I have better way to implement single instant of running app, which is by opening specific port at first load, here the code:


B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private xInstance As UDPSocket '<--- from jNetwork library
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   Try
       xInstance.Initialize("",12340,0) '<--- change the unique port as you want
       Log("Form ready")
       MainForm.Show
   Catch
       fx.Msgbox(MainForm, "Application already running !","Attention")
       ExitApplication
   End Try
End Sub

Regards,
 

JackKirk

Well-Known Member
Licensed User
Longtime User
I have better way to implement single instant of running app, which is by opening specific port at first load, here the code:


B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xInstance As UDPSocket '<--- from jNetwork library
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
Try
xInstance.Initialize("",12340,0) '<--- change the unique port as you want
Log("Form ready")
MainForm.Show
Catch
fx.Msgbox(MainForm, "Application already running !","Attention")
ExitApplication
End Try
End Sub
What a beautifully simple elegant bit of code - have implemented it on all my AWS EC2 and remote site PC B4J apps.
 
Top