LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Gtk, glib alarm(time) signal? (http://www.linuxquestions.org/questions/programming-9/gtk-glib-alarm-time-signal-462122/)

kalleanka 07-08-2006 12: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?

thanks

paulsm4 07-08-2006 03:40 PM

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

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

... and ...
Code:

// Timer callback:
gboolean
timeout_cb (gpointer data)
{
  Hbafile *hbafile = (Hbafile *)data;
  if (hbafile->isStarted ())
  {
    hbafile->nextFrame ();
    return TRUE;
  }
  else
  {
    // 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 05: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 06: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 02: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 10:13 AM.