简体   繁体   中英

Uniqueness of Gtk application without creating any window

Is it possible to run only one copy of GTK application but without creating any windows? This example allows you to run several copies of the program, but I somehow need to enforce that only one be allowed to run.

#include <gtkmm.h>                                                                                                                                 

int                                                                                                                                                
main(int argc, char** argv)                                                                                                                        
{                                                                                                                                                  
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
    app->hold();

    return app->run();                                                                                                                               
}

If I add a GTK window everything is working as I'd expect it to do. ie only one copy is running, the second one just exits. But my application lives in a system tray and doesn't create any windows at start.

#include <gtkmm.h>                                                                                                                                 

int                                                                                                                                                
main(int argc, char** argv)                                                                                                                        
{                                                                                                                                                  
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");                                                  
    Gtk::Window window;                                                                                                                              

    return app->run(window);                                                                                                                         
}

I'm not familiar with c++, but my suggestion is that you place hold method wrong. Only primary instance should be held. After second instance launched it raise 'activate' signal for primary instance. So place all you action in g_application_activate method seems like solution.

This is how it works with python, maybe this can help

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class Application(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(application_id="org.example.myapp")

    def do_activate(self):
        if not hasattr(self, "my_app_settings"):
            self.hold()
            self.my_app_settings = "Primary application instance."
            print(self.my_app_settings)
        else:
            print("Already running!")

app = Application()
app.run()

Just create a GApplication (provided by the gio) instead of a GtkApplication (provided by GTK). I'm pointing to the C API, but there should be an equivalent in GTKmm.

From that page:

Another feature that GApplication (optionally) provides is process uniqueness. Applications can make use of this functionality by providing a unique application ID. If given, only one application with this ID can be running at a time per session. The session concept is platform-dependent, but corresponds roughly to a graphical desktop login. When your application is launched again, its arguments are passed through platform communication to the already running program. The already running instance of the program is called the "primary instance"; for non-unique applications this is the always the current instance. On Linux, the D-Bus session bus is used for communication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM