When the user hits Ctrl+C in a Posix / Linux shell with a program running, that program recieves a SIGINT signal. When running a program based on GApplication, this means that the program gets terminated immediately.
How can I overcome this and have GApplication shut down gracefully?
You can use
g_unix_signal_add()
. This function takes a callback that is called once the program recieves the signal you specify. (SIGINT in this case)That callback should then call
g_application_release()
until the GApplication's use count dropped to zero. Once that is the case, the Main Loop will terminate and GApplication'sshutdown
signal will be emitted. By handling that signal you can do all necessary deinitialization tasks before the program will terminate.(taken from the reference manual:)
An example in Vala:
(compile with
valac foo.vala --pkg gio-2.0 --pkg posix
)