g_timeout_add ()

在GTK中,如果您要定时让程序去作某件事,则可以使用g_timeout_add()或g_timeout_add_full().一个例子如下:

 

这个例子的作用就是把当前时间显示到窗口中,即显示了一个实时时钟。
//~~~~~~~ begin of program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <cairo.h>
#include <gtk/gtk.h>
#include <time.h>


static char buffer[256];
/******************************
*  把buffer显示到窗口中
*  每次画窗口时调用
*/
static gboolean
on_expose_event(GtkWidget *widget,
    GdkEventExpose *event,
    gpointer data)
{
  cairo_t *cr;

  cr = gdk_cairo_create(widget->window);

  cairo_move_to(cr, 30, 30);
  cairo_show_text(cr, buffer);

  cairo_destroy(cr);

  return FALSE;
}
 
/******************************
*  把当前时间打印到buffer中,并且重画窗口
*  每次timeout后调用,即每秒调用一次
*/
static gboolean
time_handler(GtkWidget *widget)
{
  if (widget->window == NULL) return FALSE;

  time_t curtime;
  struct tm *loctime;

  curtime = time(NULL);
  loctime = localtime(&curtime);
  strftime(buffer, 256, "%T", loctime);

  gtk_widget_queue_draw(widget);
  return TRUE;
}
int
main (int argc, char *argv[])
{

  GtkWidget *window;
  GtkWidget *darea;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  darea = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER (window), darea);

  g_signal_connect(darea, "expose-event",
      G_CALLBACK(on_expose_event), NULL);   // 每次画窗口时的callback
  g_signal_connect(window, "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 170, 100);

  gtk_window_set_title(GTK_WINDOW(window), "timer");
  g_timeout_add(1000, (GSourceFunc) time_handler, (gpointer) window);
  gtk_widget_show_all(window);
  time_handler(window);

  gtk_main();

  return 0;
}
//~~~~~~~ end of program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

例子来源:

http://zetcode.com/tutorials/gtktutorial/gtkevents/

 

 

这两个函数的说明见下:

g_timeout_add ()

guint               g_timeout_add                       (guint interval,
GSourceFunc function,
gpointer data);

Sets a function to be called at regular intervals, with the default priority, G_PRIORITY_DEFAULT. The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again. The first call to the function will be at the end of the first interval.

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).

 

interval :the time between calls to the function, in milliseconds (1/1000ths of a second)
function :function to call
data :data to pass to function
Returns :the ID (greater than 0) of the event source.

第一个参数是间隔的毫秒数,第二个参数是定时后的callback,第三个是传递给callback的数据。callback的形式如下:

          gint timeout_callback(gpointer data);

g_timeout_add的返回值可以用来结束这个timeout,如下(假如返回放到tag中)

          void g_source_remove(gint tag);

也可以让callback返回0或FALSE来结束timeout。

更多的参考可见GTK+tutorial 相关章节:

http://library.gnome.org/devel/gtk-tutorial/stable/c1761.html#SEC-TIMEOUTS

 

 

g_timeout_add_full ()

guint               g_timeout_add_full                  (gint priority,
guint interval,
GSourceFunc function,
gpointer data,
GDestroyNotify notify);

Sets a function to be called at regular intervals, with the given priority. The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again. The notify function is called when the timeout is destroyed. The first call to the function will be at the end of the first interval.

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).

 

priority :the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
interval :the time between calls to the function, in milliseconds (1/1000ths of a second)
function :function to call
data :data to pass to function
notify :function to call when the idle is removed, or NULL
Returns :the ID (greater than 0) of the event source.
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
g_timeout_add函数是GLib库中的一个函数,用于在指定的时间间隔后执行一个回调函数。 其函数原型如下: ``` guint g_timeout_add(guint interval, GSourceFunc function, gpointer data); ``` 其中,参数interval表示时间间隔(以毫秒为单位),function表示要执行的回调函数,data表示传递给回调函数的数据。 g_timeout_add函数的返回值是一个guint类型的ID,可以通过该ID来取消定时器。 使用g_timeout_add函数的步骤如下: 1. 编写一个回调函数,该函数会在指定的时间间隔后被调用。 2. 调用g_timeout_add函数并传入回调函数、时间间隔和传递给回调函数的数据。 3. 在适当的时候,可以通过返回的ID取消定时器。 例如,以下代码演示了如何每隔1秒钟输出一次“Hello World”: ``` #include <glib.h> #include <stdio.h> gboolean print_hello(gpointer data) { printf("Hello World\n"); return TRUE; } int main() { guint timer_id = g_timeout_add(1000, print_hello, NULL); // 程序会在5秒钟后退出 g_timeout_add(5000, gtk_main_quit, NULL); gtk_main(); g_source_remove(timer_id); return 0; } ``` 在上述代码中,我们首先定义了一个回调函数print_hello,该函数会输出“Hello World”。然后,我们调用g_timeout_add函数来每隔1秒钟执行一次该回调函数。最后,我们调用g_source_remove函数来取消定时器。 需要注意的是,g_timeout_add函数并不是精确的定时器。如果回调函数执行的时间超过了设定的时间间隔,那么下一次回调函数的执行时间会被推迟。因此,在使用g_timeout_add函数时,应该尽量减少回调函数执行时间,以保证定时器的精度。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值