I’m building an application for a linux platform in C, with a GTK+2 GUI. I need to keep a real-time clock running on the screen displaying HH:MM:SS (only needs to be accurate to the second), and then have various functions triggered at specific times entered by the user in a GTK_ENTRY field.
I’ve been researching the best way to do this, and have found similar questions where people have answered to use GThread threading to monitor the time, but I have not been able to find any specifics.
Can I send a function that contains an endless loop, constantly checking the time over and over, to a thread? Somehow that doesn’t seem right, or the best way to handle it.
I would really appreciate any specifics anyone can offer, or any direction towards resources that might answer this question.
Thank you!
Simply add a glib timeout handler, using
g_timeout_add(). If you use a period of, say, half a second you should be fine. In the handler code, do whatever widget manipulation is needed in order to refresh the displayed time. This might be as simple as formatting some text for a GtkLabel.Of course, I don’t recommend that you use the “ticking” of the timeout callbacks as the actual clock source, you should use regular API:s to find out what time it is in each callback.