Creates a #GTask acting on source_object, which will eventually be used to invoke callback in the current [thread-default main context][g-main-context-push-thread-default].
Gets task's #GCancellable
Gets task's check-cancellable flag. See gio.task.Task.setCheckCancellable for more details.
Gets the value of #GTask:completed. This changes from false to true after the task’s callback is invoked, and will return false if called from inside the callback.
Gets the #GMainContext that task will return its result in (that is, the context that was the [thread-default main context][g-main-context-push-thread-default] at the point when task was created).
Gets task’s name. See gio.task.Task.setName.
Gets task's priority
Gets task's return-on-cancel flag. See gio.task.Task.setReturnOnCancel for more details.
Gets the source object from task. Like gio.async_result.AsyncResult.getSourceObject, but does not ref the object.
Gets task's source tag. See gio.task.Task.setSourceTag.
Gets task's task_data.
Tests if task resulted in an error.
Gets the result of task as a #gboolean.
Gets the result of task as an integer (#gssize).
Gets the result of task as a pointer, and transfers ownership of that value to the caller.
Gets the result of task as a #GValue, and transfers ownership of that value to the caller. As with gio.task.Task.returnValue, this is a generic low-level method; gio.task.Task.propagatePointer and the like will usually be more useful for C code.
Sets task's result to result and completes the task (see gio.task.Task.returnPointer for more discussion of exactly what this means).
Sets task's result to error (which task assumes ownership of) and completes the task (see gio.task.Task.returnPointer for more discussion of exactly what this means).
Checks if task's #GCancellable has been cancelled, and if so, sets task's error accordingly and completes the task (see gio.task.Task.returnPointer for more discussion of exactly what this means).
Sets task's result to result and completes the task (see gio.task.Task.returnPointer for more discussion of exactly what this means).
Sets task’s result to a new glib.error.ErrorG created from domain, code, message and completes the task.
Sets task's result to result and completes the task. If result is not null, then result_destroy will be used to free result if the caller does not take ownership of it with gio.task.Task.propagatePointer.
Sets task's result to result (by copying it) and completes the task.
Runs task_func in another thread. When task_func returns, task's #GAsyncReadyCallback will be invoked in task's #GMainContext.
Runs task_func in another thread, and waits for it to return or be cancelled. You can use gio.task.Task.propagatePointer, etc, afterward to get the result of task_func.
Sets or clears task's check-cancellable flag. If this is true (the default), then gio.task.Task.propagatePointer, etc, and gio.task.Task.hadError will check the task's #GCancellable first, and if it has been cancelled, then they will consider the task to have returned an "Operation was cancelled" error (gio.types.IOErrorEnum.Cancelled), regardless of any other error or return value the task may have had.
Sets task’s name, used in debugging and profiling. The name defaults to null.
Sets task's priority. If you do not call this, it will default to G_PRIORITY_DEFAULT.
Sets or clears task's return-on-cancel flag. This is only meaningful for tasks run via gio.task.Task.runInThread or gio.task.Task.runInThreadSync.
Sets task's source tag.
Sets task’s name, used in debugging and profiling.
Sets task's task data (freeing the existing task data, if any).
Checks that result is a #GTask, and that source_object is its source object (or that source_object is null and result has no source object). This can be used in g_return_if_fail() checks.
Creates a #GTask and then immediately calls gio.task.Task.returnError on it. Use this in the wrapper function of an asynchronous method when you want to avoid even calling the virtual method. You can then use gio.async_result.AsyncResult.isTagged in the finish method wrapper to check if the result there is tagged as having been created by the wrapper method, and deal with it appropriately if so.
Gets the source object from a #GAsyncResult.
Gets the user data from a #GAsyncResult.
Checks if res has the given source_tag (generally a function pointer indicating the function res was created by).
If res is a #GSimpleAsyncResult, this is equivalent to gio.simple_async_result.SimpleAsyncResult.propagateError. Otherwise it returns false.
Set the GObject of a D ObjectG wrapper.
Get a pointer to the underlying C object.
Calls g_object_ref() on a GObject.
Calls g_object_unref() on a GObject.
Get the GType of an object.
GObject GType property.
Convenience method to return this cast to a type. For use in D with statements.
Template to get the D object from a C GObject and cast it to the given D object type.
Connect a D closure to an object signal.
Template for setting a GObject property.
Template for getting a GObject property.
Creates a binding between source_property on source and target_property on target.
Creates a binding between source_property on source and target_property on target, allowing you to set the transformation functions to be used by the binding.
This function is intended for #GObject implementations to re-enforce a floating[floating-ref] object reference. Doing this is seldom required: all #GInitiallyUnowneds are created with a floating reference which usually just needs to be sunken by calling gobject.object.ObjectG.refSink.
Increases the freeze count on object. If the freeze count is non-zero, the emission of "notify" signals on object is stopped. The signals are queued until the freeze count is decreased to zero. Duplicate notifications are squashed so that at most one #GObject::notify signal is emitted for each property modified while the object is frozen.
Gets a named field from the objects table of associations (see gobject.object.ObjectG.setData).
Gets a property of an object.
This function gets back user data pointers stored via gobject.object.ObjectG.setQdata.
Gets n_properties properties for an object. Obtained properties will be set to values. All properties must be valid. Warnings will be emitted and undefined behaviour may result if invalid properties are passed in.
Checks whether object has a floating[floating-ref] reference.
Emits a "notify" signal for the property property_name on object.
Emits a "notify" signal for the property specified by pspec on object.
Increase the reference count of object, and possibly remove the floating[floating-ref] reference, if object has a floating reference.
Releases all references to other objects. This can be used to break reference cycles.
Each object carries around a table of associations from strings to pointers. This function lets you set an association.
Sets a property on an object.
Remove a specified datum from the object's data associations, without invoking the association's destroy handler.
This function gets back user data pointers stored via gobject.object.ObjectG.setQdata and removes the data from object without invoking its destroy() function (if any was set). Usually, calling this function is only required to update user data pointers with a destroy notifier, for example:
Reverts the effect of a previous call to gobject.object.ObjectG.freezeNotify. The freeze count is decreased on object and when it reaches zero, queued "notify" signals are emitted.
This function essentially limits the life time of the closure to the life time of the object. That is, when the object is finalized, the closure is invalidated by calling gobject.closure.Closure.invalidate on it, in order to prevent invocations of the closure with a finalized (nonexisting) object. Also, gobject.object.ObjectG.ref_ and gobject.object.ObjectG.unref are added as marshal guards to the closure, to ensure that an extra reference count is held on object during invocation of the closure. Usually, this function will be called on closures that use this object as closure data.
Connect to Notify signal.
Gets the source object from a #GAsyncResult.
Gets the user data from a #GAsyncResult.
Checks if res has the given source_tag (generally a function pointer indicating the function res was created by).
If res is a #GSimpleAsyncResult, this is equivalent to gio.simple_async_result.SimpleAsyncResult.propagateError. Otherwise it returns false.
A gio.task.Task represents and manages a cancellable ‘task’.
Asynchronous operations
The most common usage of gio.task.Task is as a gio.async_result.AsyncResult, to manage data during an asynchronous operation. You call gio.task.Task.new_ in the ‘start’ method, followed by gio.task.Task.setTaskData and the like if you need to keep some additional data associated with the task, and then pass the task object around through your asynchronous operation. Eventually, you will call a method such as gio.task.Task.returnPointer or gio.task.Task.returnError, which will save the value you give it and then invoke the task’s callback function in the thread-default main context (see glib.main_context.MainContext.pushThreadDefault) where it was created (waiting until the next iteration of the main loop first, if necessary). The caller will pass the gio.task.Task back to the operation’s finish function (as a gio.async_result.AsyncResult), and you can use gio.task.Task.propagatePointer or the like to extract the return value.
Using gio.task.Task requires the thread-default glib.main_context.MainContext from when the gio.task.Task was constructed to be running at least until the task has completed and its data has been freed.
If a gio.task.Task has been constructed and its callback set, it is an error to not call g_task_return_*() on it. GLib will warn at runtime if this happens (since 2.76).
Here is an example for using gio.task.Task as a gio.async_result.AsyncResult:
Chained asynchronous operations
gio.task.Task also tries to simplify asynchronous operations that internally chain together several smaller asynchronous operations. gio.task.Task.getCancellable, gio.task.Task.getContext, and gio.task.Task.getPriority allow you to get back the task’s gio.cancellable.Cancellable, glib.main_context.MainContext, and I/O priority
when starting a new subtask, so you don’t have to keep track of them yourself. gio.task.Task.attachSource simplifies the case of waiting for a source to fire (automatically using the correct glib.main_context.MainContext and priority).
Here is an example for chained asynchronous operations:
Asynchronous operations from synchronous ones
You can use gio.task.Task.runInThread to turn a synchronous operation into an asynchronous one, by running it in a thread. When it completes, the result will be dispatched to the thread-default main context (see glib.main_context.MainContext.pushThreadDefault) where the gio.task.Task was created.
Running a task in a thread:
Adding cancellability to uncancellable tasks
Finally, gio.task.Task.runInThread and gio.task.Task.runInThreadSync can be used to turn an uncancellable operation into a cancellable one. If you call gio.task.Task.setReturnOnCancel, passing TRUE, then if the task’s gio.cancellable.Cancellable is cancelled, it will return control back to the caller immediately, while allowing the task thread to continue running in the background (and simply discarding its result when it finally does finish). Provided that the task thread is careful about how it uses locks and other externally-visible resources, this allows you to make ‘GLib-friendly’ asynchronous and cancellable synchronous variants of blocking APIs.
Cancelling a task:
Porting from gio.simple_async_result.SimpleAsyncResult
gio.task.Task’s API attempts to be simpler than gio.simple_async_result.SimpleAsyncResult’s in several ways:
Thread-safety considerations
Due to some infelicities in the API design, there is a thread-safety concern that users of gio.task.Task have to be aware of:
If the main thread drops its last reference to the source object or the task data before the task is finalized, then the finalizers of these objects may be called on the worker thread.
This is a problem if the finalizers use non-threadsafe API, and can lead to hard-to-debug crashes. Possible workarounds include: