Asynchronous bus proxy creation

Hello,

I’m desperately trying to create a DBusProxy asynchronously through the g_dbus_proxy_new_for_bus function.

Unfortunately after calling the g_dbus_proxy_new_for_bus function it seems to be blocked and the callback (the AsyncReadyCallback callback parameter) passed to the g_dbus_proxy_new_for_bus function is never called.

Could you please provide an example ? The only example I could find were with the g_dbus_proxy_new_for_bus_sync function.

Thank you very much in advance for any help.

Do you have a running GMainContext?

Here is a simple example – it just creates a proxy, reads a property in the callback and then quits:

#include <stdio.h>
#include <glib.h>
#include <gio/gio.h>

static gboolean quit = FALSE;


static void _proxy_connected (G_GNUC_UNUSED GObject* pObj, GAsyncResult *res, G_GNUC_UNUSED gpointer ptr)
{
	GDBusProxy *proxy = g_dbus_proxy_new_for_bus_finish (res, NULL);
	if (proxy)
	{
		// We have a valid proxy -- normally, you'd store "proxy" somewhere meaningful,
		// e.g. in a global variable or in a struct passed in "ptr"
		
		// For the sake of example, just read one of its properties
		GVariant *prop = g_dbus_proxy_get_cached_property (proxy, "Features");
		// TODO: error checking!
		const gchar **res = g_variant_get_strv (prop, NULL);
		printf ("Got features:\n");
		unsigned int i;
		for (i = 0; res[i]; i++) printf ("%s\n", res[i]);
		
		g_free (res);
		g_variant_unref (prop);
		g_object_unref (proxy); // this will delete "proxy"
	}
	else fprintf (stderr, "Cannot create DBus proxy for org.freedesktop.DBus\n");
	
	quit = TRUE;
}


int main(int argc, char **argv)
{
	
	g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
		G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
		NULL, // GDBusInterfaceInfo
		"org.freedesktop.DBus",
		"/org/freedesktop/DBus",
		"org.freedesktop.DBus",
		NULL, // GCancellable
		_proxy_connected,
		NULL);
	
	GMainContext *ctx = g_main_context_default ();
	while (!quit) g_main_context_iteration (ctx, TRUE);
	
	return 0;
}

Thank you for your help.

I, indeed, forgot to create a GMainContext and thanks to @dkondor I ran the GMainLoop in another thread which also solved my problem. g_main_loop_run is a blocking function.