-   Programming (
-   -   Gtk, glib alarm(time) signal? (

kalleanka 07-08-2006 01:54 PM

Gtk, glib alarm(time) signal?
Does any one know if glib or gtk have an alarm function. If I need some code to be executed in 10 seconds. How do I do it?


paulsm4 07-08-2006 04:40 PM

Yes, gtk_timeout. You create a callback, and initiate the timer:

  // Initiate timer:
  extern gint m_timer =
    g_timeout_add (FRAME_DURATION, timeout_cb, this);

... and ...

// Timer callback:
timeout_cb (gpointer data)
  Hbafile *hbafile = (Hbafile *)data;
  if (hbafile->isStarted ())
    hbafile->nextFrame ();
    return TRUE;
    // NOTE: Returning "false" here *disables* the GTK+ timer!
    return FALSE;

If the callback returns "TRUE", the timer is reset. Otherwise, if it returns "FALSE", then it stops.

That "hbafile" stuff is irrelevant - it's just my way of passing a (C++) object reference to a (GTK+2.0/C) callback.

sundialsvcs 07-08-2006 06:27 PM

There are, generally speaking, several ways to handle "timers" in these various systems. Choose the one that fits your needs best.

One approach employs the SIGALRM signal that's issued by the operating system, catching that signal and presenting calls to one or more asynchronous callback-routines that you must define. Timers set up in this way are truly asynchronous, and can occur at any time. (And it might not be "the best" time.)

Another approach, commonly used in GUI systems, employs timer messages that are added to the regular event-queue that your application is servicing. Upon receipt of a message, one or more message-handling routines are called. The difference here is that the response is always being performed in the context of "a regular message-handling routine" and is therefore synchronous with all other types of message-handling that's going on.

Both approaches will be available, both at the same time. Review what your system provides and what your application needs.

paulsm4 07-08-2006 07:22 PM

kalleanka -

To elaborate on what sundialsvcs is saying - there are lots of ways to implement some kind of "timed interrupted": you can use the OS SIG_ALRM signal, you can "sleep()" in a busy/wait polling loop, you can call "select()" with a timeout, etc etc.

But if you've got a GUI program (like GTK+ or MS-Windows), then it's often simplest and cleanest to make use of the GUI's main event loop. Which is precisely what I did with g_timeout_add () in my example (as it happens, this example implements a little animation loop).

'Hope that helps .. PSM

kalleanka 07-09-2006 03:23 AM

Thanks a lot.

Im neew to the gtk-gui and I need a search function for it. Hawent found one.

The g_timeout_add function is exactly what I need. Since I now use the gtk, glib etc I want to stick to it.

Thanks for the code as well it helps a lot.

I love this forum.

All times are GMT -5. The time now is 03:13 PM.