B4J Question Windows TaskManager Running Processes ?

alienhunter

Active Member
Licensed User
Longtime User
hi to all ,
it is possible to see if a process is running and shut it down with B4J ?
or make sure that your app was not started twice ?

thanks AH
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Task Manager will not really help as all Java processes appear as Java.exe in Task Manager.

There are two options to force a single instance of your app. You can try to open a network port when your program starts. It will fail if the first program has already opened the port.

You can also create a "lock" file. The problem with this approach is that the file will be left if the program has crashed. You will need to implement a more complicated solution that creates a new file and deletes the old one every few minutes and write the current time to the file.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Its not just crashes. If the user turns off the computer when your program is running then the file will not be deleted and the user will not be able to start your program.

Use a timer to create a file every 10 minutes. You can then check if the file already exists whether more than 11 minutes have passed (which means that it is a leftover).
 
Upvote 0

alienhunter

Active Member
Licensed User
Longtime User
Its not just crashes. If the user turns off the computer when your program is running then the file will not be deleted and the user will not be able to start your program.

Use a timer to create a file every 10 minutes. You can then check if the file already exists whether more than 11 minutes have passed (which means that it is a leftover).

thanks erel ,
good idea thanks
the users in the company they are familiar to the lock file , a major cad/cam software uses that option , i will give them the option to delete it if present .
And you are right it just shows Java.... running even compiled to a exe in windows task manager.
....:)
But back to the processes would it be possible to see if (example acrobat.exe ) runs and how many times ..to monitor other software running ?
i did something similar in VB6 and i dont want to use VB6 anymore just stay with B4a and B4j

many thanks AH
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a bit more complicated in B4J as B4J is a cross platform solution.

You can use jShell to run the tasklist program:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   Dim js As Shell
   js.Initialize("js", "tasklist", Null)
   js.Run(60000)
End Sub

Sub js_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success Then
     Dim m As Matcher = Regex.Matcher("acrobat.exe", StdOut)
     Dim count As Int
     Do While m.Find
       count = count + 1
     Loop
     Log("Count = " & count)
   Else
     Log(LastException)
   End If
End Sub
 
Upvote 0
Top