B4J Question [JavaPackager] Single Instance

Discussion in 'B4J Questions' started by jmon, Dec 14, 2015.

  1. jmon

    jmon Well-Known Member Licensed User

    Hello,

    I switched to using JavaPackager, but I can't find how to run a single instance of my application. Before I could do it with Launch4J.

    Idea anyone?

    Regards
    Jmon.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    jmon likes this.
  3. jmon

    jmon Well-Known Member Licensed User

    Thank you.

    I made a quick function for anyone that needs it (Depends on Jshell / Windows only):
    Code:
    Sub ProcessExists(ExeName As StringAs Boolean
        
    Dim Result As ShellSyncResult
        
    Dim sh As Shell
        sh.InitializeDoNotHandleQuotes(
    """tasklist"Array As String("/FI"$""IMAGENAME eq ${ExeName}""$))
        Result = sh.RunSynchronous(
    2000)
        
    Return Not(Result.StdOut.Contains("No tasks are running"))
    End Sub
     
  4. jmon

    jmon Well-Known Member Licensed User

    Actually for what I needed, this is better:

    Code:
    Sub AppStart (Form1 As Form, Args() As String)
        
    If ProcessCount("myApp\.exe") > 1 Then
            
    Log("Only one instance of myApp allowed.")
            
    ExitApplication(2)
        
    End If
    End Sub

    Sub ProcessCount(ExeNamePattern As StringAs Int
        
    Dim Count As Int = 0
        
    Dim Result As ShellSyncResult
        
    Dim sh As Shell
        sh.InitializeDoNotHandleQuotes(
    """tasklist"Null)
        Result = sh.RunSynchronous(
    2000)
        
    If Result.Success = False Then Return Count
        
    Dim Match As Matcher  = Regex.Matcher2(ExeNamePattern, Regex.CASE_INSENSITIVE, Result.StdOut)
        
    Do While Match.Find
            Count = Count + 
    1
        
    Loop
        
    Return Count
    End Sub
    This code checks if one instance of the app is running, if it is, the close the app. Note that the Process name has to be written as a regex, so you have to escape the dot (\.).
     
    tdocs2, giga, DonManfred and 2 others like this.
  5. giga

    giga Well-Known Member Licensed User

    Good Work jmon!
     
    jmon likes this.
  6. tdocs2

    tdocs2 Well-Known Member Licensed User

    Good Work ^2, jmon!

    Your generosity is to be commended.

    Thank you.

    Sandy
     
  7. tdocs2

    tdocs2 Well-Known Member Licensed User

    Hello, jmon.

    I tried your sub in post #3, but I must be doing something wrong... Your sub returns True whether notepad.exe is running or not.

    My Code is included. Also, log is shown.

    Your help is appreciated.

    Thank you.

    Sandy

    Code:
    Sub BShell_MouseClicked (EventData As MouseEvent)
        
    Dim shellexe As String
        shellexe=
    "c:\Windows\System32\notepad.exe"
        
    Dim exerunning As Boolean
        exerunning=ProcessExists(shellexe)
        
    Log ("exerunning "&exerunning)
    End Sub

    Sub ProcessExists(ExeName As StringAs Boolean
        
    Dim Result As ShellSyncResult
        
    Dim Sh As Shell
        Sh.InitializeDoNotHandleQuotes(
    """tasklist"Array As String("/FI"$""IMAGENAME eq ${ExeName}""$))
        Result = Sh.RunSynchronous(
    2000)
        
    Log("Result.StdOut = " & Result.StdOut)
        
    Return Not(Result.StdOut.Contains("No tasks are running"))
    End Sub
    Log:
    Code:
    Program started.
    Result.StdOut =
    exerunning 
    true
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Simpler approach:
    Code:
    Sub MainForm_MouseClicked (EventData As MouseEvent)
      
    Dim shellexe As String
      shellexe=
    "notepad.exe"
      
    Dim exerunning As Boolean =ProcessExists(shellexe)
      
    Log ("exerunning "&exerunning)
    End Sub

    Sub ProcessExists(ExeName As StringAs Boolean
      
    Dim Result As ShellSyncResult
      
    Dim Sh As Shell
      Sh.InitializeDoNotHandleQuotes(
    """tasklist"Null)
      Result = Sh.RunSynchronous(
    5000)
         
    Return Result.StdOut.ToLowerCase.Contains(ExeName.ToLowerCase)
    End Sub
     
    jmon and tdocs2 like this.
  9. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Erel.

    It works as advertised.

    Now, if notepad is not running I start Notepad via shell (see modifications below).

    If it is running, how do I give it focus?

    Sandy

    Code:
    Sub BShell_MouseClicked (EventData As MouseEvent)
      
    Dim shellexe As String
      shellexe=
    "notepad.exe"
      
    Dim exerunning As Boolean =ProcessExists(shellexe)
      
    Log ("exerunning "&exerunning)
      
    If exerunning = False Then
          shell1.initialize(
    "shell1","c:\Windows\System32\notepad.exe",list1)
          shell1.Run(-
    1)
      
    End If
    End Sub

    Sub ProcessExists(ExeName As StringAs Boolean
      
    Dim Result As ShellSyncResult
      
    Dim Sh As Shell
      Sh.InitializeDoNotHandleQuotes(
    """tasklist"Null)
      Result = Sh.RunSynchronous(
    5000)
         
    Return Result.StdOut.ToLowerCase.Contains(ExeName.ToLowerCase)
    End Sub
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  11. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Erel.

    New thread posted here.

    Sandy
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice