📄 glib-the-main-event-loop.html
字号:
<p> The <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> data type represents a main event loop. A <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> is created with <a href="glib-The-Main-Event-Loop.html#g-main-loop-new"><code class="function">g_main_loop_new()</code></a>. After adding the initial event sources, <a href="glib-The-Main-Event-Loop.html#g-main-loop-run"><code class="function">g_main_loop_run()</code></a> is called. This continuously checks for new events from each of the event sources and dispatches them. Finally, the processing of an event from one of the sources leads to a call to <a href="glib-The-Main-Event-Loop.html#g-main-loop-quit"><code class="function">g_main_loop_quit()</code></a> to exit the main loop, and <a href="glib-The-Main-Event-Loop.html#g-main-loop-run"><code class="function">g_main_loop_run()</code></a> returns. </p><p> It is possible to create new instances of <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> recursively. This is often used in GTK+ applications when showing modal dialog boxes. Note that event sources are associated with a particular <a href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a>, and will be checked and dispatched for all main loops associated with that <a href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a>. </p><p> GTK+ contains wrappers of some of these functions, e.g. <ahref="../gtk/gtk-General.html#gtk-main"><code class="function">gtk_main()</code></a>, <ahref="../gtk/gtk-General.html#gtk-main-quit"><code class="function">gtk_main_quit()</code></a> and <ahref="../gtk/gtk-General.html#gtk-events-pending"><code class="function">gtk_events_pending()</code></a>. </p><div class="refsect2" lang="en"><a name="id2848378"></a><h3>Creating new sources types</h3><p> One of the unusual features of the GTK+ main loop functionality is that new types of event source can be created and used in addition to the builtin type of event source. A new event source type is used for handling GDK events. A new source type is created by <em class="firstterm">deriving</em> from the <a href="glib-The-Main-Event-Loop.html#GSource"><span class="type">GSource</span></a> structure. The derived type of source is represented by a structure that has the <a href="glib-The-Main-Event-Loop.html#GSource"><span class="type">GSource</span></a> structure as a first element, and other elements specific to the new source type. To create an instance of the new source type, call <a href="glib-The-Main-Event-Loop.html#g-source-new"><code class="function">g_source_new()</code></a> passing in the size of the derived structure and a table of functions. These <a href="glib-The-Main-Event-Loop.html#GSourceFuncs"><span class="type">GSourceFuncs</span></a> determine the behavior of the new source types. </p><p> New source types basically interact with with the main context in two ways. Their prepare function in <a href="glib-The-Main-Event-Loop.html#GSourceFuncs"><span class="type">GSourceFuncs</span></a> can set a timeout to determine the maximum amount of time that the main loop will sleep before checking the source again. In addition, or as well, the source can add file descriptors to the set that the main context checks using <a href="glib-The-Main-Event-Loop.html#g-source-add-poll"><code class="function">g_source_add_poll()</code></a>. </p></div><hr><div class="refsect2" lang="en"><a name="id2848466"></a><h3>Customizing the main loop iteration</h3><p> Single iterations of a <a href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a> can be run with <a href="glib-The-Main-Event-Loop.html#g-main-context-iteration"><code class="function">g_main_context_iteration()</code></a>. In some cases, more detailed control of exactly how the details of the main loop work is desired, for instance, when integrating the <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> with an external main loop. In such cases, you can call the component functions of <a href="glib-The-Main-Event-Loop.html#g-main-context-iteration"><code class="function">g_main_context_iteration()</code></a> directly. These functions are <a href="glib-The-Main-Event-Loop.html#g-main-context-prepare"><code class="function">g_main_context_prepare()</code></a>, <a href="glib-The-Main-Event-Loop.html#g-main-context-query"><code class="function">g_main_context_query()</code></a>, <a href="glib-The-Main-Event-Loop.html#g-main-context-check"><code class="function">g_main_context_check()</code></a> and <a href="glib-The-Main-Event-Loop.html#g-main-context-dispatch"><code class="function">g_main_context_dispatch()</code></a>. </p><p> The operation of these functions can best be seen in terms of a state diagram, as shown in <a href="glib-The-Main-Event-Loop.html#mainloop-states" title="Figure 1. States of a Main Context">Figure 1, “States of a Main Context”</a>. </p><div class="figure"><a name="mainloop-states"></a><p class="title"><b>Figure 1. States of a Main Context</b></p><div><img src="mainloop-states.gif" alt="States of a Main Context"></div></div></div></div><div class="refsect1" lang="en"><a name="id2848596"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2848602"></a><h3><a name="GMainLoop"></a>GMainLoop</h3><a class="indexterm" name="id2848612"></a><pre class="programlisting">typedef struct _GMainLoop GMainLoop;</pre><p>The <span class="structname">GMainLoop</span> struct is an opaque data type representing the main event loop of a GLib or GTK+ application.</p></div><hr><div class="refsect2" lang="en"><a name="id2848633"></a><h3><a name="g-main-loop-new"></a>g_main_loop_new ()</h3><a class="indexterm" name="id2848644"></a><pre class="programlisting"><a href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a>* g_main_loop_new (<a href="glib-The-Main-Event-Loop.html#GMainContext">GMainContext</a> *context, <a href="glib-Basic-Types.html#gboolean">gboolean</a> is_running);</pre><p>Creates a new <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> structure.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>context</code></em> :</span></td><td> a <a href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a> (if <code class="literal">NULL</code>, the default context will be used).</td></tr><tr><td><span class="term"><em class="parameter"><code>is_running</code></em> :</span></td><td> set to <code class="literal">TRUE</code> to indicate that the loop is running. Thisis not very important since calling <a href="glib-The-Main-Event-Loop.html#g-main-loop-run"><code class="function">g_main_loop_run()</code></a> will set this to<code class="literal">TRUE</code> anyway.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td> a new <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2848780"></a><h3><a name="g-main-loop-ref"></a>g_main_loop_ref ()</h3><a class="indexterm" name="id2848791"></a><pre class="programlisting"><a href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a>* g_main_loop_ref (<a href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a> *loop);</pre><p>Increases the reference count on a <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> object by one.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>loop</code></em> :</span></td><td> a <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a></td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td> <em class="parameter"><code>loop</code></em></td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2848873"></a><h3><a name="g-main-loop-unref"></a>g_main_loop_unref ()</h3><a class="indexterm" name="id2848884"></a><pre class="programlisting">void g_main_loop_unref (<a href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a> *loop);</pre><p>Decreases the reference count on a <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> object by one. Ifthe result is zero, free the loop and free all associated memory.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>loop</code></em> :</span></td><td> a <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a></td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2848950"></a><h3><a name="g-main-loop-run"></a>g_main_loop_run ()</h3><a class="indexterm" name="id2848961"></a><pre class="programlisting">void g_main_loop_run (<a href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a> *loop);</pre><p>Runs a main loop until <a href="glib-The-Main-Event-Loop.html#g-main-loop-quit"><code class="function">g_main_loop_quit()</code></a> is called on the loop.If this is called for the thread of the loop's <a href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a>,it will process events from the loop, otherwise it willsimply wait.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>loop</code></em> :</span></td><td> a <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a></td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2849040"></a><h3><a name="g-main-loop-quit"></a>g_main_loop_quit ()</h3><a class="indexterm" name="id2849050"></a><pre class="programlisting">void g_main_loop_quit (<a href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a> *loop);</pre><p>Stops a <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> from running. Any calls to <a href="glib-The-Main-Event-Loop.html#g-main-loop-run"><code class="function">g_main_loop_run()</code></a>for the loop will return.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>loop</code></em> :</span></td><td> a <a href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a></td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2849127"></a><h3><a name="g-main-loop-is-running"></a>g_main_loop_is_running ()</h3><a class="indexterm" name="id2849137"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gboolean">gboolean</a> g_main_loop_is_running (<a href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a> *loop);</pre><p>Checks to see if the main loop is currently being run via <a href="glib-The-Main-Event-Loop.html#g-main-loop-run"><code class="function">g_main_loop_run()</code></a>.</p><p></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -