在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.

g_timeout_add ()相关推荐

  1. C语言基于GTK+Libvlc实现的简易视频播放器(二)

    简易视频播放器-全屏播放 一.课程说明 上一次我们使用gtk+libvlc实现了一个最简单的视频播放器,可以实现点击按钮暂定和停止播放视频,以及同步显示视频播放进度,但即使作为一个视频播放器,只有这些 ...

  2. oFono学习笔记——GATChat(2):发送AT命令

    摘要: 本文主要描述了GAtChat如何发送AT命令的全过程 1. GAtChat AT命令发送接口 在GAtChat库当中,根据AT命令返回结果的不同,GAtChat定义了四种不同的发送接口:一般发 ...

  3. GTK实现:俄罗斯方块小游戏源代码(RussiaCube.c)

    /* * File: RussiaCube.c* Author: ldd 版权归该作者所有,本人做转载声明,如有侵权请联系LZ** Created on 2012年1月8日, 下午12:38 */#i ...

  4. GTK+图形化应用程序开发学习笔记(五)—组装盒、组合表、固定容器构件

    GTK+图形化应用程序开发学习笔记(五)-组装盒.组合表.固定容器构件 一.组装盒 组装盒(GtkBox)也称为组合构件.使用组装盒可以将多个构件放在一个容器中.容器可以把组装盒看作是一个构件.不像按 ...

  5. GTK+图形化应用程序开发学习笔记(二)—Glib库

    在学习GTK+之前我们需要先学习一下 glib的有关知识.因为我们将会在以后的学习中遇到这些知识.由于本笔记不是专门介绍glib的,所以下面的介绍不会太详细.   一.什么是glib库        ...

  6. GTK构件 tree_view

    这次网络管理工具的编写主要使用GTK作为画图工具:在这里回顾一下其中用到的tree_view构件 GtkTreeView 构件是一个高级的构件,利用他可以制作出漂亮的普通列表或者是树状的列表:这个构件 ...

  7. linux - glib使用

    glib 简介 命令行参数解析 事件源 简介 源码下载 glib库是Linux平台下最常用的C语言函数库,它具有很好的可移植性和实用性 编译链接:gcc g_init.c pkg-config --l ...

  8. Cairo图形指南(6)

    透明 这一篇讲述有关透明的一些基本知识,并提供两个有趣的透明效果. 透明是透过某种材质的可见度.理解透明最简单的方式就是想像一下玻璃或者水.从技术上讲,光线可以穿过玻璃,因此我们可以看到玻璃之后的物体 ...

  9. cairosvg在linux中的安装_Cairo编程

    一.简介 cairo 是一个免费的矢量绘图软件库,它可以绘制多种输出格式.cairo 支持许多平台,包括 Linux.BSD.Microsoft® Windows® 和 OSX(BeOS 和 OS2 ...

最新文章

  1. 洛谷P1550 [USACO08OCT]打井Watering Hole
  2. 万能的Python,不仅能开发效率高,还能绘画各种漫画图像
  3. WINCE6.0+S3C6410主要时钟控制
  4. Auto Encoder再学习
  5. 运动检测(前景检测)之(一)ViBe
  6. php公众号客服消息图文,微信公众号开发系列-发送客服消息(示例代码)
  7. python的基础_python基础知识,python必背内容,一、python的基
  8. php sort函数,php中sort函数的功能起什么作用呢?
  9. svn利用钩子脚本功能实现代码同步到web目录
  10. liferay6连接mysql_Liferay 6开发学习(二十六):数据库连接相关问题
  11. 64bit 用户空间内核空间
  12. ae导出gif插件_有了它,AE终于可以直接导出gif动图啦!
  13. 7-55 查询水果价格
  14. 如何在python中输入复杂的数学公式_如何快速输入复杂的数学公式?这里有 3 个实用技巧...
  15. 一张图带你解读--如何从零开始学习接口自动化
  16. 【YOLOV5-5.x 源码解读】google_utils.py
  17. 优质的石材 石头VRay材质球素材推荐,不容错过
  18. 解读,投资的天使轮,A轮,B轮,C轮是啥意思
  19. 解决mysql sum求和返回null问题或IFNULL应用
  20. 增值电信业务经营许可证怎么续期,需要什么材料

热门文章

  1. Unity Shader总结(十)——Cubemap、镜子、玻璃、程序纹理
  2. 西门子S7-1200与FUNUC机器人MODBUS TCP通信
  3. ico格式的计算机图标,ICO(Windows的图标文件格式)_百度百科
  4. 大三软件工程小项目-小技术集合-大纲
  5. 江苏省计算机一级在线考,江苏计算机等级考试一级成绩查询
  6. leetcode 叠罗汉 面试题17.06
  7. 代账行业急需标准,良莠不齐成为过去!
  8. “软件源”是什么?常用软件源有哪些?
  9. 如何下载廊坊市卫星地图高清版大图
  10. 微信商城的机遇wemall