Linux设计g_timeout_add ()仓酷云
常常有些朋友在Linux论坛问一些问题,不过,其中大多数的问题都是很基的。在GTK中,假如您要准时让程序往作某件事,则可使用g_timeout_add()或g_timeout_add_full().一个例子以下:
这个例子的感化就是把以后工夫显现到窗口中,即显现了一个及时时钟。//~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#include<cairo.h>#include<gtk/gtk.h>#include<time.h>staticcharbuffer;/*******************************把buffer显现到窗口中*每次画窗口时挪用*/staticgbooleanon_expose_event(GtkWidget*widget,GdkEventExpose*event,gpointerdata){cairo_t*cr;cr=gdk_cairo_create(widget->window);cairo_move_to(cr,30,30);cairo_show_text(cr,buffer);cairo_destroy(cr);returnFALSE;}/*******************************把以后工夫打印到buffer中,而且重画窗口*每次timeout后挪用,即每秒挪用一次*/staticgbooleantime_handler(GtkWidget*widget){if(widget->window==NULL)returnFALSE;time_tcurtime;structtm*loctime;curtime=time(NULL);loctime=localtime(&curtime);strftime(buffer,256,"%T",loctime);gtk_widget_queue_draw(widget);returnTRUE;}//~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~0//~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1例子
http://zetcode.com/tutorials/gtktutorial/gtkevents/
这两个函数的申明见下:
g_timeout_add()
//~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2Setsafunctiontobecalledatregularintervals,withthedefaultpriority,G_PRIORITY_DEFAULT.ThefunctioniscalledrepeatedlyuntilitreturnsFALSE,atwhichpointthetimeoutisautomaticallydestroyedandthefunctionwillnotbecalledagain.Thefirstcalltothefunctionwillbeattheendofthefirstinterval.
Notethattimeoutfunctionsmaybedelayed,duetotheprocessingofothereventsources.Thustheyshouldnotbereliedonforprecisetiming.Aftereachcalltothetimeoutfunction,thetimeofthenexttimeoutisrecalculatedbasedonthecurrenttimeandthegiveninterval(itdoesnottrytocatchuptimelostindelays).
interval:thetimebetweencallstothefunction,inmilliseconds(1/1000thsofasecond)function:functiontocalldata:datatopasstofunctionReturns:theID(greaterthan0)oftheeventsource.
第一个参数是距离的毫秒数,第二个参数是准时后的callback,第三个是传送给callback的数据。callback的情势以下:
ginttimeout_callback(gpointerdata);
g_timeout_add的前往值能够用来停止这个timeout,以下(假设前往放到tag中)
voidg_source_remove(ginttag);
也能够让callback前往0或FALSE来停止timeout。
更多的参考可见GTK+tutorial相干章节:
http://library.gnome.org/devel/gtk-tutorial/stable/c1761.html#SEC-TIMEOUTS
g_timeout_add_full()
//~~~~~~~beginofprogram~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~3Setsafunctiontobecalledatregularintervals,withthegivenpriority.ThefunctioniscalledrepeatedlyuntilitreturnsFALSE,atwhichpointthetimeoutisautomaticallydestroyedandthefunctionwillnotbecalledagain.Thenotifyfunctioniscalledwhenthetimeoutisdestroyed.Thefirstcalltothefunctionwillbeattheendofthefirstinterval.
Notethattimeoutfunctionsmaybedelayed,duetotheprocessingofothereventsources.Thustheyshouldnotbereliedonforprecisetiming.Aftereachcalltothetimeoutfunction,thetimeofthenexttimeoutisrecalculatedbasedonthecurrenttimeandthegiveninterval(itdoesnottrytocatchuptimelostindelays).
priority:thepriorityoftheidlesource.TypicallythiswillbeintherangebetweenG_PRIORITY_DEFAULT_IDLEandG_PRIORITY_HIGH_IDLE.interval:thetimebetweencallstothefunction,inmilliseconds(1/1000thsofasecond)function:functiontocalldata:datatopasstofunctionnotify:functiontocallwhentheidleisremoved,orNULLReturns:theID(greaterthan0)oftheeventsource.
学习python,无论你是打算拿他当主要开发语言,还是当辅助开发语言,你都应该学习他,因为有些时间我们耗不起。 最好先搜寻一下论坛是否有您需要的文章。这样可以获得事半功倍的效果。 Linux操作系统这个名词记得在很早以前就听过,但当时并不知道具体是什么样的操作系统,只知道是一个与嵌入式密切相关的操作系统。 最好先搜寻一下论坛是否有您需要的文章。这样可以获得事半功倍的效果。 首先Linux是开源的,这也是最主要的原因,想学windows,Unix,对不起我们没源代码。也正是因为这样,Linux才能够像滚雪球一样越滚越大,发展到现在这种规模。 你需要提供精确有效的信息。Linux这并不是要求你简单的把成吨的出错代码或者数据完全转储摘录到你的提问中。 熟读写基础知识,学得会不如学得牢。 查阅经典工具书和Howto,特别是Howto是全球数以万计的Linux、Unix的经验总结非常有参考价值通常40%的问题同样可以解决。
页:
[1]