diff --git a/vala/live.gnome.org_tutorial/async_bg_thread_example.vala b/vala/live.gnome.org_tutorial/async_bg_thread_example.vala new file mode 100644 index 0000000..558ef18 --- /dev/null +++ b/vala/live.gnome.org_tutorial/async_bg_thread_example.vala @@ -0,0 +1,43 @@ +// Async method to run a slow calculation in a background thread. +// Build with: valac --thread --pkg=gio-2.0 example.vala + +async double do_calc_in_bg(double val) throws ThreadError { + SourceFunc callback = do_calc_in_bg.callback; + double[] output = new double[1]; + + // Hold reference to closure to keep it from being freed whilst + // thread is active. + ThreadFunc run = () => { + // Perform a dummy slow calculation. + // (Insert real-life time-consuming algorithm here.) + double result = 0; + for (int a = 0; a<10000000; a++) + result += val * a; + + // Pass back result and schedule callback + output[0] = result; + Idle.add((owned) callback); + return null; + }; + Thread.create(run, false); + + // Wait for background thread to schedule our callback + yield; + return output[0]; +} + +void main(string[] args) { + var loop = new MainLoop(); + do_calc_in_bg.begin(0.001, (obj, res) => { + try { + double result = do_calc_in_bg.end(res); + stderr.printf(@"Result: $result\n"); + } catch (ThreadError e) { + string msg = e.message; + stderr.printf(@"Thread error: $msg\n"); + } + loop.quit(); + }); + loop.run(); +} + diff --git a/vala/live.gnome.org_tutorial/async_generator_example.vala b/vala/live.gnome.org_tutorial/async_generator_example.vala new file mode 100644 index 0000000..ba81cda --- /dev/null +++ b/vala/live.gnome.org_tutorial/async_generator_example.vala @@ -0,0 +1,53 @@ +// Build with: valac --pkg=gio-2.0 example.vala + +abstract class Generator { + private bool consumed; + private unowned G value; + private SourceFunc callback; + + public Generator () { + helper (); + } + + private async void helper () { + yield generate (); + consumed = true; + } + + protected abstract async void generate (); + + protected async void feed (G value) { + this.value = value; + this.callback = feed.callback; + yield; + } + + public bool next () { + return !consumed; + } + + public G get () { + var result = value; + callback (); + return result; + } + + public Generator iterator () { + return this; + } +} + +class IntGenerator : Generator { + protected override async void generate () { + for (int i=0; i < 10; i++) { + if (i%2 ==0) yield feed (i); + } + } +} + +void main(string[] args) { + var gen = new IntGenerator(); + + foreach (var item in gen) + stdout.printf(@"Result: $item\n"); +} diff --git a/vala/live.gnome.org_tutorial/async_gio_example.vala b/vala/live.gnome.org_tutorial/async_gio_example.vala new file mode 100644 index 0000000..83be961 --- /dev/null +++ b/vala/live.gnome.org_tutorial/async_gio_example.vala @@ -0,0 +1,31 @@ +// Example with GIO asynchronous methods: +// Build with: valac --pkg=gio-2.0 example.vala + +async void list_dir() { + var dir = File.new_for_path (Environment.get_home_dir()); + try { + var e = yield dir.enumerate_children_async( + FileAttribute.STANDARD_NAME, 0, Priority.DEFAULT, null); + while (true) { + var files = yield e.next_files_async( + 10, Priority.DEFAULT, null); + if (files == null) { + break; + } + foreach (var info in files) { + print("%s\n", info.get_name()); + } + } + } catch (Error err) { + warning("Error: %s\n", err.message); + } +} + +void main() { + var loop = new MainLoop(); + list_dir.begin((obj, res) => { + list_dir.end(res); + loop.quit(); + }); + loop.run(); +} diff --git a/vala/live.gnome.org_tutorial/run.sh b/vala/live.gnome.org_tutorial/build.sh similarity index 61% rename from vala/live.gnome.org_tutorial/run.sh rename to vala/live.gnome.org_tutorial/build.sh index 7d4832d..518077e 100755 --- a/vala/live.gnome.org_tutorial/run.sh +++ b/vala/live.gnome.org_tutorial/build.sh @@ -8,3 +8,7 @@ valac --thread async-queue-test.vala valac --pkg gio-2.0 dbus-demo-service.vala valac main_loop.vala + +valac --pkg gio-2.0 async_gio_example.vala +valac --pkg gio-2.0 async_bg_thread_example.vala +valac --pkg gio-2.0 async_generator_example.vala