Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing multiple instances of a GTK application

Tags:

c

gtk

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.

like image 837
user1935430 Avatar asked Sep 28 '13 22:09

user1935430


2 Answers

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.

like image 178
ebassi Avatar answered Nov 14 '22 13:11

ebassi


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.

like image 31
hauzer Avatar answered Nov 14 '22 11:11

hauzer