2
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
4
<!ENTITY % local.common.attrib "xmlns:xi CDATA #FIXED 'http://www.w3.org/2003/XInclude'">
5
<!ENTITY version SYSTEM "version.xml">
7
<refentry id="GAsyncResult">
9
<refentrytitle role="top_of_page" id="GAsyncResult.top_of_page">GAsyncResult</refentrytitle>
10
<manvolnum>3</manvolnum>
11
<refmiscinfo>GIO Library</refmiscinfo>
15
<refname>GAsyncResult</refname>
16
<refpurpose>Asynchronous Function Results</refpurpose>
19
<refsynopsisdiv id="GAsyncResult.synopsis" role="synopsis">
20
<title role="synopsis.title">Synopsis</title>
24
#include <gio/gio.h>
26
<link linkend="GAsyncResult-struct">GAsyncResult</link>;
27
<link linkend="GAsyncResultIface">GAsyncResultIface</link>;
28
<link linkend="void">void</link> (<link linkend="GAsyncReadyCallback">*GAsyncReadyCallback</link>) (<link linkend="GObject">GObject</link> *source_object,
29
<link linkend="GAsyncResult">GAsyncResult</link> *res,
30
<link linkend="gpointer">gpointer</link> user_data);
31
<link linkend="gpointer">gpointer</link> <link linkend="g-async-result-get-user-data">g_async_result_get_user_data</link> (<link linkend="GAsyncResult">GAsyncResult</link> *res);
32
<link linkend="GObject">GObject</link> * <link linkend="g-async-result-get-source-object">g_async_result_get_source_object</link> (<link linkend="GAsyncResult">GAsyncResult</link> *res);
36
<refsect1 id="GAsyncResult.object-hierarchy" role="object_hierarchy">
37
<title role="object_hierarchy.title">Object Hierarchy</title>
39
<link linkend="GInterface">GInterface</link>
44
<refsect1 id="GAsyncResult.prerequisites" role="prerequisites">
45
<title role="prerequisites.title">Prerequisites</title>
48
<link linkend="GObject">GObject</link>.</para>
53
<refsect1 id="GAsyncResult.implementations" role="implementations">
54
<title role="implementations.title">Known Implementations</title>
56
GAsyncResult is implemented by
57
<link linkend="GSimpleAsyncResult">GSimpleAsyncResult</link>.</para>
63
<refsect1 id="GAsyncResult.description" role="desc">
64
<title role="desc.title">Description</title>
66
Provides a base class for implementing asynchronous function results.
69
Asynchronous operations are broken up into two separate operations
70
which are chained together by a <link linkend="GAsyncReadyCallback"><type>GAsyncReadyCallback</type></link>. To begin
71
an asynchronous operation, provide a <link linkend="GAsyncReadyCallback"><type>GAsyncReadyCallback</type></link> to the
72
asynchronous function. This callback will be triggered when the
73
operation has completed, and will be passed a <link linkend="GAsyncResult"><type>GAsyncResult</type></link> instance
74
filled with the details of the operation's success or failure, the
75
object the asynchronous function was started for and any error codes
76
returned. The asynchronous callback function is then expected to call
77
the corresponding "<link linkend="finish"><function>_finish()</function></link>" function, passing the object the
78
function was called for, the <link linkend="GAsyncResult"><type>GAsyncResult</type></link> instance, and (optionally)
79
an <parameter>error</parameter> to grab any error conditions that may have occurred.
82
The "<link linkend="finish"><function>_finish()</function></link>" function for an operation takes the generic result
83
(of type <link linkend="GAsyncResult"><type>GAsyncResult</type></link>) and returns the specific result that the
84
operation in question yields (e.g. a <link linkend="GFileEnumerator"><type>GFileEnumerator</type></link> for a
85
"enumerate children" operation). If the result or error status of the
86
operation is not needed, there is no need to call the "<link linkend="finish"><function>_finish()</function></link>"
87
function; GIO will take care of cleaning up the result and error
88
information after the <link linkend="GAsyncReadyCallback"><type>GAsyncReadyCallback</type></link> returns. Applications may
89
also take a reference to the <link linkend="GAsyncResult"><type>GAsyncResult</type></link> and call "<link linkend="finish"><function>_finish()</function></link>"
90
later; however, the "<link linkend="finish"><function>_finish()</function></link>" function may be called at most once.
93
Example of a typical asynchronous operation flow:
94
<informalexample><programlisting>
95
void _theoretical_frobnitz_async (Theoretical *t,
97
GAsyncReadyCallback *cb,
100
gboolean _theoretical_frobnitz_finish (Theoretical *t,
105
frobnitz_result_func (GObject *source_object,
109
gboolean success = FALSE;
111
success = _theoretical_frobnitz_finish (source_object, res, NULL);
114
g_printf ("Hurray!\n");
116
g_printf ("Uh oh!\n");
118
/<!-- -->* ... *<!-- -->/
122
int main (int argc, void *argv[])
124
/<!-- -->* ... *<!-- -->/
126
_theoretical_frobnitz_async (theoretical_data,
128
frobnitz_result_func,
131
/<!-- -->* ... *<!-- -->/
133
</programlisting></informalexample>
136
The callback for an asynchronous operation is called only once, and is
137
always called, even in the case of a cancelled operation. On cancellation
138
the result is a <link linkend="G-IO-ERROR-CANCELLED--CAPS"><literal>G_IO_ERROR_CANCELLED</literal></link> error.
141
Some asynchronous operations are implemented using synchronous calls.
142
These are run in a separate thread, if <link linkend="GThread"><type>GThread</type></link> has been initialized, but
143
otherwise they are sent to the Main Event Loop and processed in an idle
144
function. So, if you truly need asynchronous operations, make sure to
145
initialize <link linkend="GThread"><type>GThread</type></link>.</para>
150
<refsect1 id="GAsyncResult.details" role="details">
151
<title role="details.title">Details</title>
152
<refsect2 id="GAsyncResult-struct" role="struct">
153
<title>GAsyncResult</title>
154
<indexterm zone="GAsyncResult-struct"><primary sortas="AsyncResult">GAsyncResult</primary></indexterm><programlisting>typedef struct _GAsyncResult GAsyncResult;</programlisting>
156
Holds results information for an asynchronous operation,
157
usually passed directly to a asynchronous <link linkend="finish"><function>_finish()</function></link> operation.</para>
160
<refsect2 id="GAsyncResultIface" role="struct">
161
<title>GAsyncResultIface</title>
162
<indexterm zone="GAsyncResultIface"><primary sortas="AsyncResultIface">GAsyncResultIface</primary></indexterm><programlisting>typedef struct {
163
GTypeInterface g_iface;
167
gpointer (* get_user_data) (GAsyncResult *async_result);
168
GObject * (* get_source_object) (GAsyncResult *async_result);
172
Interface definition for <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.</para>
174
</para><variablelist role="struct">
176
<term><link linkend="GTypeInterface">GTypeInterface</link> <structfield>g_iface</structfield>;</term>
177
<listitem><simpara> The parent interface.
178
</simpara></listitem>
181
<term><structfield>get_user_data</structfield> ()</term>
182
<listitem><simpara> Gets the user data passed to the callback.
183
</simpara></listitem>
186
<term><structfield>get_source_object</structfield> ()</term>
187
<listitem><simpara> Gets the source object that issued the asynchronous operation.
188
</simpara></listitem>
190
</variablelist></refsect2>
191
<refsect2 id="GAsyncReadyCallback" role="function">
192
<title>GAsyncReadyCallback ()</title>
193
<indexterm zone="GAsyncReadyCallback"><primary sortas="AsyncReadyCallback">GAsyncReadyCallback</primary></indexterm><programlisting><link linkend="void">void</link> (*GAsyncReadyCallback) (<link linkend="GObject">GObject</link> *source_object,
194
<link linkend="GAsyncResult">GAsyncResult</link> *res,
195
<link linkend="gpointer">gpointer</link> user_data);</programlisting>
197
Type definition for a function that will be called back when an asynchronous
198
operation within GIO has been completed.</para>
200
</para><variablelist role="params">
201
<varlistentry><term><parameter>source_object</parameter> :</term>
202
<listitem><simpara> the object the asynchronous operation was started with.
203
</simpara></listitem></varlistentry>
204
<varlistentry><term><parameter>res</parameter> :</term>
205
<listitem><simpara> a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.
206
</simpara></listitem></varlistentry>
207
<varlistentry><term><parameter>user_data</parameter> :</term>
208
<listitem><simpara> user data passed to the callback.
209
</simpara></listitem></varlistentry>
210
</variablelist></refsect2>
211
<refsect2 id="g-async-result-get-user-data" role="function">
212
<title>g_async_result_get_user_data ()</title>
213
<indexterm zone="g-async-result-get-user-data"><primary sortas="async_result_get_user_data">g_async_result_get_user_data</primary></indexterm><programlisting><link linkend="gpointer">gpointer</link> g_async_result_get_user_data (<link linkend="GAsyncResult">GAsyncResult</link> *res);</programlisting>
215
Gets the user data from a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.</para>
217
</para><variablelist role="params">
218
<varlistentry><term><parameter>res</parameter> :</term>
219
<listitem><simpara> a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.
220
</simpara></listitem></varlistentry>
221
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara> the user data for <parameter>res</parameter>.
222
</simpara></listitem></varlistentry>
223
</variablelist></refsect2>
224
<refsect2 id="g-async-result-get-source-object" role="function">
225
<title>g_async_result_get_source_object ()</title>
226
<indexterm zone="g-async-result-get-source-object"><primary sortas="async_result_get_source_object">g_async_result_get_source_object</primary></indexterm><programlisting><link linkend="GObject">GObject</link> * g_async_result_get_source_object (<link linkend="GAsyncResult">GAsyncResult</link> *res);</programlisting>
228
Gets the source object from a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.</para>
230
</para><variablelist role="params">
231
<varlistentry><term><parameter>res</parameter> :</term>
232
<listitem><simpara> a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>
233
</simpara></listitem></varlistentry>
234
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara> a new reference to the source object for the <parameter>res</parameter>,
235
or <link linkend="NULL--CAPS"><literal>NULL</literal></link> if there is none.
236
</simpara></listitem></varlistentry>
237
</variablelist></refsect2>
243
<refsect1 id="GAsyncResult.see-also">
244
<title>See Also</title>