#include <glib.h>
#define INTERVALS 10 //間隔10ms
#define COUNTER 1000 //計時間隔數
GMainLoop *loop;
gint count = COUNTER;
gboolean callback(gpointer arg)
{
count--;
if( 0 == count%100 )
{
printf("%d \n", count/100);
}
if ( 0 == count )
{
g_main_loop_quit(loop);
return FALSE;
}
return TRUE;
}
int main()
{
if( 0 == g_thread_supported() )
{
g_thread_init(NULL);
}
loop = g_main_loop_new(NULL, FALSE);
g_timeout_add(INTERVALS, callback, NULL); /*每隔INTERVALS秒鐘調用一次回調函數,當調用COUNTER次後,執行g_main_loop_quit(loop),退出事件循環*/
g_main_loop_run(loop);
g_main_loop_unref(loop);
}