I have written a GTK application in C on my Linux system. At present, I can open or start multiple instances of my application. How can I modify the code so that only one instance at a maximum can run at a time?
I know that there are many ways to achieve this. One is to use an X selection; another is to use a pipe/lock file; yet another one is to use D-Bus.
I want to use D-Bus, but I don't know how to.
you can use GtkApplication
, which provides you with single-instance support by default.
if you cannot use GtkApplication
then you can use libunique
(which is deprecated by GtkApplication
but works fine with both GTK+ 2.x and GTK+ 3.x): https://wiki.gnome.org/LibUnique
alternatively, you can implement the same system used by both GtkApplication
and libunique
, which is based on DBus: you should acquire a well-known name for your application in the first instance, and if something is already holding that well-known name, meaning that there already is an instance running, then exit from your application.
GtkApplication is what you want. Basically, all you need to do is pass an unique string to gtk_application_new(). The page of its base class, GApplication, explains the details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With