a sandwhich
a sandwhich

Reputation: 4448

Multitask with gtk

How would I run a constant process in the background while there is a gtk system tray icon running? Would I just start two threads and launch the process with one and the system tray icon? Or is there a better way? Sorry, but I am somewhat new to gtk.

Upvotes: 0

Views: 1168

Answers (2)

kalev
kalev

Reputation: 1925

If I understand it correctly, then you have an application sitting in the system tray and it needs to periodically check for an external condition.

Your GUI thread can't block for a long time or it would become unresponsive.

I can think of three techniques to solve this:

  • Use a timer to periodically poll from the main (GUI) thread (g_timeout_add() or similar).
  • Create a separate thread which runs a busy-wait loop (check for the condition; sleep; check; rinse and repeat). Glib has support for thread abstraction which you could use; example GThread usage in Brasero.
  • Use asyncronous IO to check for the condition. If you are monitoring a file or directory for changes, then you could use GFileMonitor from GIO.

Upvotes: 4

liberforce
liberforce

Reputation: 11454

I don't think you need any threads in your example. What do you exactly call a "constant process"?

Either it is:

  • a blocking processing function you made, an you can do your processing in a callback that will be called when your program is idle, by splitting it in several parts (see g_idle_add and an example of lazy loading)
  • or it is what is commmonly called a process (with a PID), and as it runs in a completely separate process, you don't need threads either. Read the official documentation to learn how to spawn a process from a GTK application.

Upvotes: 0

Related Questions