Preventing Multiple Instances of a GTK Application

I wrote a GTK application in C on my Linux system. I can currently open or run multiple instances of my application. How can I change the code so that only one instance can work at a time?

I know that there are many ways to achieve this. One of them is to use choice X; another is to use the pipe / lock file; another is to use D-Bus.

I want to use D-Bus, but I do not know how to do it.

+6
source share
2 answers

you can use GtkApplication , which by default provides support for one instance.

if you cannot use GtkApplication , then you can use libunique (which has deprecated GtkApplication , but works fine with GTK + 2.x and GTK + 3.x): https://wiki.gnome.org/LibUnique

you can implement the same system that is used by both GtkApplication and libunique , which is based on DBus: you must acquire a known name for your application in the first instance, and if something is already running this famous name means that the instance is already running , then exit the application.

+6
source

GtkApplication is what you want. Basically, all you have to do is pass a unique string to . The page of its base class, GApplication , explains the details.

+1
source

Source: https://habr.com/ru/post/954856/


All Articles