How to stop multiple instances
Posted
#1
(In Topic #1375)
Regular

For some reason I find multiple instances of a program are being opened so how can I prevent more than one instance being loaded?
Posted
Regular

Impedire l'avvio di un programma.gambas già avviato - Gambas-it.org - Wikipedia
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Guru

See attached project.
if the program is running it sends the arguments to the running process instead of launching the program again.
You can test it in a terminal by running it, then running it a second time with args
Also I have just added this exact thing to my GambasProcWatch program add DBus Quit and Activate commands and auto activate running program if attempted to run again (d08d1bd9) · Commits · Bruce Steers / gambasprocwatch · GitLab
I added a DBus Activate() and Quit() method
Now if you run GambasProcWatch a second time it activates the first one instead.
This way also gives you a way to control your programs from other programs using their interfaces, commands like Activate/ Quit / open some window / run some function / etc
Now from another program i can simply do this to activate GambasProcWatch…
Or quit it…
or you can simply use pgrep to check more than one instance but it is not as useful (and i think not as reliable) as using DBus.
Code (gambas)
- sRet = Split(File.Load("/proc/" &/ Application.Id &/ "cmdline"), "\0", Null, True).Join(" ") ' this programs full command line
- ' exact match (pgrep -xf <cmdline>) the whole command line to process IDs
- ' (better to use the whole command as just the application name can give other results like the IDE or a terminal cd'd to the project directory, anything containing the project name in it's title)
- [attachment]972[/attachment]
Posted
Regular

The pgrep check doesn't always work, neither does vuott's suggestion.
Can't quite get my head around the SingleBus example yet. The example works fine, if I change the name pf the project then I get an error "-1 unable to create dbus interface" because the dbBus registration(line 19) fails with error message "Cannot enable observer".
Regards
Bill
Posted
Guru

bill-lancaster said
Thanks for the ideas, have been fiddling with this on and off for a while now without success.
The pgrep check doesn't always work, neither does vuott's suggestion.
Can't quite get my head around the SingleBus example yet. The example works fine, if I change the name pf the project then I get an error "-1 unable to create dbus interface" because the dbBus registration(line 19) fails with error message "Cannot enable observer".
Regards
Bill
there are some chars that cannot be used in a DBus interface name (I only replace spaces for underscore) so the problem may be in what you name the application.
Just change the "Application.Name" in SingleInstance.class:_new() to the name of your choice then the Application.Name will not matter.
Posted
Guru

Code (gambas)
- Return iCount
1 guest and 0 members have just viewed this.


