Last active
July 2, 2017 06:08
-
-
Save arteymix/8f9570dcaae75035758c9b5f7ba5f0a8 to your computer and use it in GitHub Desktop.
Example of non-blocking execution in GTK with Vala (run with `vala --pkg=gtk+-3 example.vala`)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Gtk; | |
Button button; | |
public static int main (string[] args) { | |
Gtk.init (ref args); | |
var window = new Window (); | |
window.title = "Count without blocking the UI"; | |
window.border_width = 10; | |
window.window_position = WindowPosition.CENTER; | |
window.set_default_size (350, 70); | |
window.destroy.connect (Gtk.main_quit); | |
button = new Button.with_label ("Start counting"); | |
button.clicked.connect (() => { | |
count (); | |
}); | |
window.add (button); | |
window.show_all (); | |
Gtk.main (); | |
return 0; | |
} | |
async void count(){ | |
for(int i = 0; i < 10000; i++){ | |
button.label = i.to_string(); | |
Idle.add (count.callback); | |
yield; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment