LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-08-2006, 12:54 PM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Rep: Reputation: 38
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
 
Old 07-08-2006, 03:40 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.

Last edited by paulsm4; 07-08-2006 at 03:49 PM.
 
Old 07-08-2006, 05:27 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,263

Rep: Reputation: 1082Reputation: 1082Reputation: 1082Reputation: 1082Reputation: 1082Reputation: 1082Reputation: 1082Reputation: 1082
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.
 
Old 07-08-2006, 06:22 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 07-08-2006 at 06:23 PM.
 
Old 07-09-2006, 02:23 AM   #5
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Original Poster
Rep: Reputation: 38
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
alarm/notify utility on GTK Jessii Linux - Software 2 05-25-2006 05:12 AM
GTK+ programs on Windows without having to install GTK+, GLib, etc Nylex Programming 2 02-19-2006 01:33 PM
Freerock uninstall stole GTK looking for up-to-date pango, atk, gtk, glib and xft tgz Oholiab Slackware 8 09-18-2005 11:57 AM
can't install gtk 2.4.4: "Requested 'glib-2.0 >= 2.4.0' but version of GLib is 2.2.3 webazoid Linux - Software 0 07-15-2004 06:07 PM
no signal when starting xorg for the 1st time (but the second time works fine) bungalowbill Linux - Software 0 06-04-2004 09:56 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration