Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Gtk;
- public long fibonacci (int n)
- {
- if (n < 2) {
- return n;
- } else {
- return (fibonacci(n - 1) + fibonacci(n - 2));
- }
- }
- /*long process of much processing*/
- public async long long_process (int val)
- {
- SourceFunc callback = long_process.callback;
- long number = 0;
- ThreadFunc<void*> run = () => {
- number = fibonacci (val);
- Idle.add((owned) callback);
- return null;
- };
- Thread.create<void*>(run, false);
- yield;
- return number;
- }
- public static int main (string[] args)
- {
- Gtk.init (ref args);
- var window = new Window ();
- window.title = "Fibonacci GTK+";
- window.set_border_width (12);
- window.set_default_size (70, 70);
- window.set_position (Gtk.WindowPosition.CENTER);
- window.destroy.connect (Gtk.main_quit);
- var headerbar = new HeaderBar ();
- var entry = new Entry ();
- var button = new Button.with_label ("Calculate");
- var label = new Label ("Result");
- var box = new Box (Orientation.VERTICAL, 0);
- headerbar.title = "Fibonacci GTK+";
- headerbar.show_close_button = true;
- box.pack_start (entry, false, true, 0);
- box.pack_start (label, false, true, 12);
- box.pack_start (button, false, true, 0);
- window.set_titlebar (headerbar);
- window.add (box);
- window.show_all ();
- /*** LOGIC ***/
- button.clicked.connect ( ()=> {
- /*here it called a long process that freezes the user interface*/
- int val = int.parse(entry.get_text());
- var loop = new MainLoop();
- long_process.begin(val, (obj, res) => {
- try {
- double result = long_process.end(res);
- label.set_label(result.to_string ());
- } catch (ThreadError e) {
- string msg = e.message;
- stderr.printf(@"Thread error: $msg\n");
- }
- loop.quit();
- });
- loop.run();
- });
- Gtk.main ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement