Re: [Vala] Asynchronous construction
- From: Jens Georg <mail jensge org>
- To: tomw ubilix com
- Cc: vala-list gnome org
- Subject: Re: [Vala] Asynchronous construction
- Date: Tue, 27 Mar 2012 11:59:44 +0200
Hi,
[Freenect C init sample ]
Trying to implement that in Vala, I took the approach below.
It seems however, that the init_async does not work. Looking at the
generated C-Code it looks like the g_async_initable_init_finish method
is called right after init_async an not with in the callback as expected.
As a result the code is crashing in the underlying libusb due to
incorrect initialization.
What am I missing here? Any idea how to solve the issue.
thanks and sorry for the lengthy post,
--tomw
---------------------------------------------------------
using GLib;
using GFreenect;
public class Main : Object
{
public GFreenect.Device device;
public Cancellable cancellable;
public Main () {
Cancellable cancellable = new Cancellable ();
}
public async void run (out Device dev) {
debug ("Starting");
Device device = (Device) GLib Object new (
typeof (Device),
"index", 0,
"subdevices", Subdevice.CAMERA
);
debug ("Device created");
Idle.add (run.callback);
^^ No need for this since you're using yield in the function. Might
actually be harmful, causing a race between the init_async you call and
the one Vala calls implicitly.
try {
debug ("Trying to run init_async");
bool re = yield device.init_async (Priority.DEFAULT, cancellable);
^^ this yield returns to main loop and once init_async calls its
call-back will call init_finish for you. So after this line the device
is initialized.
if (re) {
debug ("Result = OK");
} else {
debug ("Result = FAILED");
}
} catch (Error e) {
debug ("Error initalizing device : %s", e.message);
}
}
public void callback (Main? source, GLib.AsyncResult? res, void* user_data) {
debug ("Entering Callback");
Device dev;
if (res != null) {
source.run.end (res, out dev);
debug ("Callback called - Device created");
try {
Device kinect = new dev.finish(res);
^^ And here you're basically trying to create another device.
} catch (Error e) {
debug ("Error finalizing device : %s", e.message);
}
} else {
debug ("No result delivered");
}
}
I would do it like this:
using GLib;
using GFreenect;
public class Main : Object
{
public GFreenect.Device device;
public Cancellable cancellable;
public Main () {
Cancellable cancellable = new Cancellable ();
}
public async void run () {
debug ("Starting");
Device device = (Device) GLib Object new (
typeof (Device),
"index", 0,
"subdevices", Subdevice.CAMERA
);
debug ("Device created");
try {
debug ("Trying to run init_async");
bool re = yield device.init_async (Priority.DEFAULT, cancellable);
// start using the device
} catch (Error e) {
// re will always be true in the try block if you always get an error
// when it's false, because you end up here anyway.
debug ("Error initalizing device : %s", e.message);
}
}
static int main (string[] args) {
var loop = new MainLoop ();
var app = new Main ();
app.run.begin ();
loop.run ();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]