[Gambas-user] Single instance application, anyone else done it?
bsteers4 at gmail.com
Wed Jul 7 15:18:55 CEST 2021
On Wed, 7 Jul 2021 at 13:11, Tony Morehen <tmorehen at ajm-software.com> wrote:
> 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
> 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)
> 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 :)
> ----[ 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
> TEXTEDITOR_DBUS_INTERFACE].Open($file.Name, $file.ReadOnly,
> $file.LineNumber, $file.ColumnNumber)
> Try DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH,
> Return 'exit, replace with Me.Close if in startup form
> 'set up to receive pass-off commands
> DBus.Name = TEXTEDITOR_DBUS_NAME
> hDbusObj = New DbusObj
> DBus.Session.Register(hDbusObj, TEXTEDITOR_PATH,
> fEditor.ShowModal() 'wait until editing is all done, skip if in startup
> 'clean-up and exit, place remaining code in Form_Close if in startup form
> If Not IsNull(hDbusObj) And If DBus.IsRegistered(hDbusObj) Then
> The remaining code goes into a class file named dbusobj.
> ' Gambas class file
> Inherits DBusObject
> Public Sub org_ajmconsulting_TextEditor_Show()
> Public Sub org_ajmconsulting_TextEditor_Open(sFilePath As String,
> bReadOnly As Boolean, nLine As Integer, nColumn As Integer)
> fEditor.AddEditor(sFilePath, bReadOnly, nLine, nColumn)
Excellent thank you Tony :)
I was so close lol, things are working now though thanks to your help.
Much appreciated :)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the User