[Gambas-user] Single instance application, anyone else done it?

Tony Morehen tmorehen at ajm-software.com
Wed Jul 7 14:00:52 CEST 2021


On 2021-07-07 5:24 a.m., Bruce Steers wrote:
> Hi all,
> I'm looking for a sure fire recommended way to make an application a 
> "Single Instance" application that takes filepath args.
>
> The Reason:
> If i create a desktop launcher for my app and drop multiple files on 
> it linux does not launch the application once and send all the dropped 
> file paths to it.  it launches the application multiple times all with 
> one file arg.
> So i needed a way to only let one app launch and the other instances 
> just send the file args to the first instance.
>
> Currently i have used a pipe and Shell "pgrep -f AppName" to list all 
> app id's the lowest app id is selected as the working app and any 
> other instances of the app launched are quickly closed after sending 
> their file args to the pipe.
> Then the first app picks up the file args from the pipe file.
> (example app here... 
> https://forum.gambas.one/viewtopic.php?f=13&t=1149 
> <https://forum.gambas.one/viewtopic.php?f=13&t=1149>)
>
> I'm currently looking into DBus as a better alternative for detecting 
> an already loaded app and sending it args but i'm making slow progress 
> with DBus atm (any simple app DBus communication examples would be handy)
>
> Just wondered if anyone here has done this sort of thing already and 
> may have any advice / examples.
>
> Cheers in advance :)
> BruceS
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----


I have the following code in a startup module.  It could go in a startup 
form as well.

Private hDbusObj As DbusObj

Const TEXTEDITOR_DBUS_NAME As String = "org.ajmconsulting.TextEditor"
Const TEXTEDITOR_PATH As String = "/org/ajmconsulting/TextEditor"
Const TEXTEDITOR_DBUS_INTERFACE As String = "org.ajmconsulting.TextEditor"

Public Sub Main()    'Form_Open if startup form
     'check if gbTE is already open
     If DBus.Session.Applications.Exist(TEXTEDITOR_DBUS_NAME) Then
       'yes it is, so pass-off current file list
       If Files.Count > 0 Then
         For Each $file In Files
           DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH, 
TEXTEDITOR_DBUS_INTERFACE].Open($file.Name, $file.ReadOnly, 
$file.LineNumber, $file.ColumnNumber)
         Next
       Endif
       Try DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH, 
TEXTEDITOR_DBUS_INTERFACE].Show()
       Return    'exit, replace with Me.Close if in startup form
     Endif
     'set up to receive pass-off commands
     DBus.Name = TEXTEDITOR_DBUS_NAME
     hDbusObj = New DbusObj
     DBus.Session.Register(hDbusObj, TEXTEDITOR_PATH, 
TEXTEDITOR_DBUS_INTERFACE])
   Endif

   fEditor.ShowModal()  'wait until editing is all done, skip if in 
startup form

   'clean-up and exit, place remaining code in Form_Close if in startup form
   If Not IsNull(hDbusObj) And If DBus.IsRegistered(hDbusObj) Then
     DBus.Unregister(hDbusObj)
   Endif

End

The remaining code goes into a class file named dbusobj.

' Gambas class file

Inherits DBusObject

Public Sub org_ajmconsulting_TextEditor_Show()

   fEditor.Show()

End

Public Sub org_ajmconsulting_TextEditor_Open(sFilePath As String, 
bReadOnly As Boolean, nLine As Integer, nColumn As Integer)

   fEditor.AddEditor(sFilePath, bReadOnly, nLine, nColumn)
   fEditor.Raise()

End

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210707/1fa6aa65/attachment.htm>


More information about the User mailing list