⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk_tut-17.html

📁 gtk是linux一款强大的夸平台的图形化开发工具
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>GTK v1.2 Tutorial: Timeouts, IO and Idle Functions</TITLE> <LINK HREF="gtk_tut-18.html" REL=next> <LINK HREF="gtk_tut-16.html" REL=previous> <LINK HREF="gtk_tut.html#toc17" REL=contents></HEAD><BODY BGCOLOR="#FFFFFF"><A HREF="gtk_tut-18.html">Next</A><A HREF="gtk_tut-16.html">Previous</A><A HREF="gtk_tut.html#toc17">Contents</A><HR NOSHADE><H2><A NAME="sec_timeouts"></A> <A NAME="s17">17. Timeouts, IO and Idle Functions</A></H2><H2><A NAME="ss17.1">17.1 Timeouts</A></H2><P>You may be wondering how you make GTK do useful work when in gtk_main.Well, you have several options. Using the following functions you cancreate a timeout function that will be called every "interval"milliseconds.<P><BLOCKQUOTE><CODE><PRE>gint gtk_timeout_add( guint32     interval,                      GtkFunction function,                      gpointer    data );</PRE></CODE></BLOCKQUOTE><P>The first argument is the number of milliseconds between calls to yourfunction. The second argument is the function you wish to have called,and the third, the data passed to this callback function. The returnvalue is an integer "tag" which may be used to stop the timeout bycalling:<P><BLOCKQUOTE><CODE><PRE>void gtk_timeout_remove( gint tag );</PRE></CODE></BLOCKQUOTE><P>You may also stop the timeout function by returning zero or FALSE fromyour callback function. Obviously this means if you want your functionto continue to be called, it should return a non-zero value,i.e. TRUE.<P>The declaration of your callback should look something like this:<P><BLOCKQUOTE><CODE><PRE>gint timeout_callback( gpointer data );</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss17.2">17.2 Monitoring IO</A></H2><P>A nifty feature of GDK (the library that underlies GTK), is theability to have it check for data on a file descriptor for you (asreturned by open(2) or socket(2)). This is especially useful fornetworking applications. The function:<P><BLOCKQUOTE><CODE><PRE>gint gdk_input_add( gint              source,                    GdkInputCondition condition,                    GdkInputFunction  function,                    gpointer          data );</PRE></CODE></BLOCKQUOTE><P>Where the first argument is the file descriptor you wish to havewatched, and the second specifies what you want GDK to look for. Thismay be one of:<P><UL><LI>GDK_INPUT_READ - Call your function when there is data ready forreading on your file descriptor.</LI><LI>GDK_INPUT_WRITE - Call your function when the file descriptor isready for writing.</LI></UL><P>As I'm sure you've figured out already, the third argument is thefunction you wish to have called when the above conditions aresatisfied, and the fourth is the data to pass to this function.<P>The return value is a tag that may be used to stop GDK from monitoringthis file descriptor using the following function.<P><BLOCKQUOTE><CODE><PRE>void gdk_input_remove( gint tag );</PRE></CODE></BLOCKQUOTE><P>The callback function should be declared as:<P><BLOCKQUOTE><CODE><PRE>void input_callback( gpointer          data,                     gint              source,                      GdkInputCondition condition );</PRE></CODE></BLOCKQUOTE><P>Where <CODE>source</CODE> and <CODE>condition</CODE> are as specified above.<P><H2><A NAME="ss17.3">17.3 Idle Functions</A></H2><P>What if you have a function you want called when nothing else ishappening ?<P><BLOCKQUOTE><CODE><PRE>gint gtk_idle_add( GtkFunction function,                   gpointer    data );</PRE></CODE></BLOCKQUOTE><P>This causes GTK to call the specified function whenever nothing elseis happening.<P><BLOCKQUOTE><CODE><PRE>void gtk_idle_remove( gint tag );</PRE></CODE></BLOCKQUOTE><P>I won't explain the meaning of the arguments as they follow very muchlike the ones above. The function pointed to by the first argument togtk_idle_add will be called whenever the opportunity arises. As withthe others, returning FALSE will stop the idle function from beingcalled.<P><HR NOSHADE><A HREF="gtk_tut-18.html">Next</A><A HREF="gtk_tut-16.html">Previous</A><A HREF="gtk_tut.html#toc17">Contents</A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -