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

Bruce Steers bsteers4 at gmail.com
Thu Jul 8 15:56:41 CEST 2021


On Thu, 8 Jul 2021 at 10:51, Bruce Steers <bsteers4 at gmail.com> wrote:

>
>
> On Wed, 7 Jul 2021 at 19:06, Benoît Minisini <g4mba5 at gmail.com> wrote:
>
>> Le 07/07/2021 à 15:18, Bruce Steers a écrit :
>> >
>> > Excellent thank you Tony :)
>> > I was so close lol, things are working now though thanks to your help.
>> >
>> > Much appreciated :)
>> > BruceS
>> >
>>
>> Actually doing that:
>>
>> If DBus.Session.Applications.Exist(TEXTEDITOR_DBUS_NAME) Then
>>
>> to know if you are already on the bus is wrong, because it's not atomic.
>>
>> You must instead set the DBus.Unique property to TRUE before
>> registering, so that a unique name is used.
>>
>> That way, if you register on the DBus bus again, you will get an error,
>> and it will be atomic.
>>
>> So you should do instead:
>>
>> DBus.Unique = TRUE
>>
>> hDbusObj = New DbusObj
>>
>> ' Registering the first object automatically registers the application.
>>
>> Try DBus.Session.Register(hDbusObj, TEXTEDITOR_PATH,
>> TEXTEDITOR_DBUS_INTERFACE])
>>
>> If Error Then
>>
>>    ' We suppose we are already registered.
>>
>>    If Files.Count > 0 Then
>>      For Each $file In Files
>>        DBus[DBus.Name][TEXTEDITOR_PATH,
>> TEXTEDITOR_DBUS_INTERFACE].Open($file.Name, $file.ReadOnly,
>> $file.LineNumber, $file.ColumnNumber)
>>      Next
>>    Endif
>>
>>    Try DBus[DBus.Name][TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE].Show()
>>
>> Endif
>>
>> Tell me if it actually works!
>>
>
>
> Seems problematic though maybe.
> To suppose an Error means the interface exists is not right.
> Error.code is always -1 so that's no help
> Error.text differs (bad address / unique address already registered/ etc)
> but is not safe to use due to languages.
> Is it possible to get the GError value?
>
> https://dbus.freedesktop.org/doc/dbus-glib/dbus-glib-DBusGError.html#DBusGError
>
> otherwise i will have to use the
> If DBus.Session.Applications.Exist(DBUS_NAME) Then
> just to be sure that's the error i got.
> Regards
> BruceS
>

Nope it does not work using DBus.Session.Applications.Exist() , it works if
one app is ready loaded but launching 2 apps together fails to connect for
the second but Applications[] is not yet updated.

seems all i can do is test error.Text ends with "already registered" (im
not sure if gambas translates that msg)

Another option on a gambas level is to edit the DBus.class gb.dbus and add
a boolean property "ApplicationExists" that gets set to true if the
connection fails due to already existing interface during
_RegisterApplication()

Or if Error.Code could be something other than -1 if connection exists?

then i can tell if error was because connection exists or something else.

BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210708/b3850646/attachment.htm>


More information about the User mailing list