GTK g_timeout_add function
来源:互联网 发布:智慧树网络课程怎么上 编辑:程序博客网 时间:2024/06/05 16:42
It is very interesting for Gtk to set a timer to do some things by the gtk framework. Here is a paper on thishttp://caterpillar.on
Besides, it is also interesting for QT has the familiar mechanism with qtimer class.
如果您要定時讓程式去作某件事,則可以使用g_timeout_add()或g_timeout_add_full(),g_timeout_add()的定義如下:
guint g_timeout_add(guint interval,
GSourceFunc function,
gpointer da
第一個參數是時間間隔,以毫秒為單位,第二個參數是時間到的回呼函式,第三個參數是傳給回呼函式的資料,以 內建 Signal 的發射與停止中的範例來說,可以使用g_timeout_add()改寫如下而執行結果相同:
- g_timeout_demo.c
#include <gtk/gtk.h>
gboolean timeout_callback(GtkButton *button) {
static gint count = 0;
if(count < 5) {
g_signal_emit_by_name(button, "clicked");
count++;
return TRUE;
}
else {
return FALSE;
}
}
// 自訂Callback函式
void button_clicked(GtkWidget *button, gpointer da
g_print("按鈕按下:%s\n", (char *) da
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "哈囉!GTK+!");
button = gtk_button_new_with_label("按我");
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(GTK_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(GTK_OBJECT(button), "clicked",
G_CALLBACK(button_clicked), "哈囉!按鈕!");
g_timeout_add(1000, (GSourceFunc) timeout_callback, button);
gtk_widget_show(window);
gtk_widget_show(button);
gtk_main();
return 0;
}
在回呼函式中,若傳回TRUE則繼續下一次計時,計時器的下一次計時,會是在回呼函式執行完畢後開始,傳回FALSE則計時器結束並自動銷毀,若您使用g_timeout_add_full():
guint g_timeout_add_full(gint priority,
guint interval,
GSourceFunc function,
gpointer da
GDestroyNotify notify);
第一個參數為時間到時的執行優先權,可以設定的優先權如下:
- G_PRIORITY_HIGH
- G_PRIORITY_DEFAULT(預設)
- G_PRIORITY_HIGH_IDLE
- G_PRIORITY_DEFAULT_IDLE
- G_PRIORITY_LOW
最後一個參數則是計時器被移除時要執行的函式。
相對於計時重複執行某個動作,您可以使用g_idle_add()或g_idle_add_full()函式,讓程式在沒有什麼事情作的時候(例如沒有任何使用者操作,沒有任何需要運算的程式碼時),也可以作一些事情,若使用g_idle_add():
guint g_idle_add(GSourceFunc function,
gpointer da
第一個參數是回呼函式,第二個參數是傳遞給回呼函式的資料,例如下面這個範例,在使用者不作任何事時,就會執行指定的idle函式,而按下按鈕時就執行按鈕的回呼函式:
- g_idle_demo.c
#include <gtk/gtk.h>
gboolean idle_callback(gpointer da
g_print("%s。。XD\n", da
return TRUE;
}
void button_pressed(GtkButton *button, gpointer da
int i;
for(i = 0; i < 10; i++) {
g_print("%s...\n", da
sleep(1);
}
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GTimer");
gtk_window_set_default_size(GTK_WINDOW(window), 150, 50);
button = gtk_button_new_with_label("按我");
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(GTK_OBJECT(button), "clicked",
G_CALLBACK(button_pressed), "do something");
g_signal_connect(GTK_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_idle_add((GSourceFunc) idle_callback, "無事可作");
gtk_widget_show_all(window);
gtk_main();
return 0;
}
同樣的,指定的idle函式若傳回FALSE則會移除idle功能,若是使用g_idle_add_full():
guint g_idle_add_full(gint priority,
GSourceFunc function,
gpointer da
GDestroyNotify notify);
其上的參數與g_timeout_add_full()類似。
- GTK g_timeout_add function
- g_timeout_add ()
- g_timeout_add用法
- g_timeout_add 不是多线程
- gtk
- GTK+
- gtk.....
- GTK +
- GTK
- GTK+
- gtk
- gtk+
- gtk
- gtk
- GTK
- gtk
- The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support.
- Function
- 申请借款验证
- 杂七杂八(1)图论部分
- 数据库基础
- springMvc 中让人纠结的org.hibernate.HibernateException: No Session found for current thread
- 简单地使用sym*函数族导出函数栈(Based on x86)
- GTK g_timeout_add function
- 关于推荐系统
- Sparsity and Some Basics of L1 Regularization
- 感受营销的乐趣(一)--什么是成都最牛串串
- u盘启动安装xp/win7系统详解
- linux下串口设备
- win7蓝牙连android
- android之HttpPost&HttpGet使用方法介绍
- 算法效果AB测试中的PV-UV不对称性