Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Gtk;
- public class MyThread : Object{
- public long n { get; private set; }
- public MyThread(long n){
- this.n = n;
- }
- public long run(){
- long number;
- number = this.fibonacci(this.n);
- Thread.exit (42);
- return number;
- }
- public long fibonacci (long i){
- if (i < 2) {
- return i;
- } else {
- stdout.printf("Running! \n");
- return (this.fibonacci(i - 1) + fibonacci(i - 2));
- }
- }
- }
- public class Application : Gtk.Application {
- public Application(){
- Object(application_id:"testing.my.application", flags: ApplicationFlags.FLAGS_NONE);
- }
- protected override void activate(){
- Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this);
- 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 loop = new MainLoop();
- var headerbar = new Gtk.HeaderBar ();
- var entry = new Gtk.Entry ();
- var button = new Gtk.Button.with_label ("Calculate");
- var label = new Gtk.Label ("Result");
- var box = new Gtk.Box (Gtk.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 ( ()=> {
- try {
- // Start a thread:
- MyThread my_thread = new MyThread (long.parse (entry.get_text ()));
- Thread<long> thread = new Thread<long>.try ("My fst. thread", my_thread.run);
- // Wait until thread finishes:
- long result = thread.join ();
- label.set_label (result.to_string ());
- } catch (Error e) {
- stdout.printf ("Error: %s\n", e.message);
- }
- });
- }
- }
- public static int main (string[] args){
- if (Thread.supported () == false) {
- stderr.printf ("Threads are not supported!\n");
- return -1;
- }
- var app = new Application();
- app.run(args);
- Gtk.main ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement