<?xml version="1.0"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" 
               "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
<!ENTITY version SYSTEM "version.xml">
]>
<refentry id="GAsyncResult">
<refmeta>
<refentrytitle role="top_of_page" id="GAsyncResult.top_of_page">GAsyncResult</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GIO Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>GAsyncResult</refname>
<refpurpose>Asynchronous Function Results</refpurpose>
</refnamediv>

<refsynopsisdiv id="GAsyncResult.synopsis" role="synopsis">
<title role="synopsis.title">Synopsis</title>

<synopsis>

#include &lt;gio/gio.h&gt;

                    <link linkend="GAsyncResult-struct">GAsyncResult</link>;
                    <link linkend="GAsyncResultIface">GAsyncResultIface</link>;
<link linkend="void">void</link>                (<link linkend="GAsyncReadyCallback">*GAsyncReadyCallback</link>)              (<link linkend="GObject">GObject</link> *source_object,
                                                         <link linkend="GAsyncResult">GAsyncResult</link> *res,
                                                         <link linkend="gpointer">gpointer</link> user_data);
<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);
<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);
</synopsis>
</refsynopsisdiv>

<refsect1 id="GAsyncResult.object-hierarchy" role="object_hierarchy">
<title role="object_hierarchy.title">Object Hierarchy</title>
<synopsis>
  <link linkend="GInterface">GInterface</link>
   +----GAsyncResult
</synopsis>
</refsect1>

<refsect1 id="GAsyncResult.prerequisites" role="prerequisites">
<title role="prerequisites.title">Prerequisites</title>
<para>
GAsyncResult requires
 <link linkend="GObject">GObject</link>.</para>
</refsect1>



<refsect1 id="GAsyncResult.implementations" role="implementations">
<title role="implementations.title">Known Implementations</title>
<para>
GAsyncResult is implemented by
 <link linkend="GSimpleAsyncResult">GSimpleAsyncResult</link>.</para>
</refsect1>




<refsect1 id="GAsyncResult.description" role="desc">
<title role="desc.title">Description</title>
<para>
Provides a base class for implementing asynchronous function results.
</para>
<para>
Asynchronous operations are broken up into two separate operations
which are chained together by a <link linkend="GAsyncReadyCallback"><type>GAsyncReadyCallback</type></link>. To begin
an asynchronous operation, provide a <link linkend="GAsyncReadyCallback"><type>GAsyncReadyCallback</type></link> to the 
asynchronous function. This callback will be triggered when the 
operation has completed, and will be passed a <link linkend="GAsyncResult"><type>GAsyncResult</type></link> instance 
filled with the details of the operation's success or failure, the 
object the asynchronous function was started for and any error codes 
returned. The asynchronous callback function is then expected to call 
the corresponding "<link linkend="finish"><function>_finish()</function></link>" function with the object the function 
was called for, and the <link linkend="GAsyncResult"><type>GAsyncResult</type></link> instance, and optionally, 
an <parameter>error</parameter> to grab any error conditions that may have occurred.
</para>
<para>
The purpose of the "<link linkend="finish"><function>_finish()</function></link>" function is to take the generic 
result of type <link linkend="GAsyncResult"><type>GAsyncResult</type></link> and return the specific result
that the operation in question yields (e.g. a <link linkend="GFileEnumerator"><type>GFileEnumerator</type></link> for
a "enumerate children" operation). If the result or error status
of the operation is not needed, there is no need to call the
"<link linkend="finish"><function>_finish()</function></link>" function, GIO will take care of cleaning up the
result and error information after the <link linkend="GAsyncReadyCallback"><type>GAsyncReadyCallback</type></link> 
returns. It is also allowed to take a reference to the <link linkend="GAsyncResult"><type>GAsyncResult</type></link> and
call "<link linkend="finish"><function>_finish()</function></link>" later.
</para>
<para>
Example of a typical asynchronous operation flow:
<informalexample><programlisting>
void _theoretical_frobnitz_async (Theoretical         *t, 
                                  GCancellable        *c, 
                                  GAsyncReadyCallback *cb,
                                  gpointer             u);

gboolean _theoretical_frobnitz_finish (Theoretical   *t,
                                       GAsyncResult  *res,
                                       GError       **e);

static void 
frobnitz_result_func (GObject      *source_object, 
		 GAsyncResult *res, 
		 gpointer      user_data)
{
  gboolean success = FALSE;

  success = _theoretical_frobnitz_finish (source_object, res, NULL);

  if (success)
    g_printf ("Hurray!\n");
  else 
    g_printf ("Uh oh!\n");

  /<!-- -->* ... *<!-- -->/

}

int main (int argc, void *argv[])
{
   /<!-- -->* ... *<!-- -->/

   _theoretical_frobnitz_async (theoretical_data, 
                                NULL, 
                                frobnitz_result_func, 
                                NULL);

   /<!-- -->* ... *<!-- -->/
}
</programlisting></informalexample>
</para>
<para>
The callback for an asynchronous operation is called only once, and is
always called, even in the case of a cancelled operation. On cancellation
the result is a <link linkend="G-IO-ERROR-CANCELLED--CAPS"><literal>G_IO_ERROR_CANCELLED</literal></link> error.
</para>
<para>
Some ascynchronous operations are implemented using synchronous calls. These
are run in a separate thread, if <link linkend="GThread"><type>GThread</type></link> has been initialized, but otherwise they
are sent to the Main Event Loop and processed in an idle function. So, if you
truly need asynchronous operations, make sure to initialize <link linkend="GThread"><type>GThread</type></link>.</para>
<para>
</para>
</refsect1>

<refsect1 id="GAsyncResult.details" role="details">
<title role="details.title">Details</title>
<refsect2 id="GAsyncResult-struct" role="struct">
<title>GAsyncResult</title>
<indexterm zone="GAsyncResult-struct"><primary sortas="AsyncResult">GAsyncResult</primary></indexterm><programlisting>typedef struct _GAsyncResult GAsyncResult;</programlisting>
<para>
Holds results information for an asynchronous operation,
usually passed directly to a asynchronous <link linkend="finish"><function>_finish()</function></link> operation.</para>
<para>
</para></refsect2>
<refsect2 id="GAsyncResultIface" role="struct">
<title>GAsyncResultIface</title>
<indexterm zone="GAsyncResultIface"><primary sortas="AsyncResultIface">GAsyncResultIface</primary></indexterm><programlisting>typedef struct {
  GTypeInterface g_iface;

  /* Virtual Table */

  gpointer  (* get_user_data)     (GAsyncResult *async_result);
  GObject * (* get_source_object) (GAsyncResult *async_result);
} GAsyncResultIface;
</programlisting>
<para>
Interface definition for <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.</para>
<para>
</para><variablelist role="struct">
<varlistentry>
<term><link linkend="GTypeInterface">GTypeInterface</link>&#160;<structfield>g_iface</structfield>;</term>
<listitem><simpara> The parent interface.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><structfield>get_user_data</structfield>&#160;()</term>
<listitem><simpara> Gets the user data passed to the callback.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><structfield>get_source_object</structfield>&#160;()</term>
<listitem><simpara> Gets the source object that issued the asynchronous operation.
</simpara></listitem>
</varlistentry>
</variablelist></refsect2>
<refsect2 id="GAsyncReadyCallback" role="function">
<title>GAsyncReadyCallback ()</title>
<indexterm zone="GAsyncReadyCallback"><primary sortas="AsyncReadyCallback">GAsyncReadyCallback</primary></indexterm><programlisting><link linkend="void">void</link>                (*GAsyncReadyCallback)              (<link linkend="GObject">GObject</link> *source_object,
                                                         <link linkend="GAsyncResult">GAsyncResult</link> *res,
                                                         <link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Type definition for a function that will be called back when an asynchronous
operation within GIO has been completed.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>source_object</parameter>&#160;:</term>
<listitem><simpara> the object the asynchronous operation was started with.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>res</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&#160;:</term>
<listitem><simpara> user data passed to the callback.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-async-result-get-user-data" role="function">
<title>g_async_result_get_user_data ()</title>
<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>
<para>
Gets the user data from a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>res</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the user data for <parameter>res</parameter>. 
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="g-async-result-get-source-object" role="function">
<title>g_async_result_get_source_object ()</title>
<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>
<para>
Gets the source object from a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>res</parameter>&#160;:</term>
<listitem><simpara> a <link linkend="GAsyncResult"><type>GAsyncResult</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the source object for the <parameter>res</parameter>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>

</refsect1>



<refsect1 id="GAsyncResult.see-also">
<title>See Also</title>
#GSimpleAsyncResult
</refsect1>

</refentry>
