1

I have working code without a GUI which manages some threads. Now I want to integrate it with a GUI created in GTK. It builds the GUI with test_glade which makes use of gtk::main:

fn main() {

    //..
    test_glade();
    //..

    // Question about this
    while estado < 4 {

        if *count.lock().unwrap() == arc_pref.cantidad {
            estado = 3;     
        }else if estado == 2{
            estado = 1;
        }else if estado == 1{
            estado = 2;
        }else if *count.lock().unwrap() == 0 && estado == 3{
            estado = 1;
        }
        //..

        estados(count.clone(), arc_pref.clone(), estado).join();
    }

}

fn test_glade(){

    gtk::init();  
    //..
    window.show_all();  
    gtk::main();//main loop gtk
}
//..

After looking, I see that this is the main loop, so my code does not run.

//..
test_glade();
//..

// Question about this
while estado < 4 {
    //..
    estados(count.clone(), arc_pref.clone(), estado).join();
}

How can I integrate my code which uses some threads into the GTK main loop?

For now I have created a new thread, and I have put the code inside the thread, and it seems to work:

fn test_glade(){

    gtk::init();  
    //..
    thread::spawn(move|| {
        //code Question

    });
    //..
    window.show_all();  
    gtk::main();//main loop gtk
}
//..
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Angel Angel
  • 19,670
  • 29
  • 79
  • 105
  • 2
    Take a look at [g_idle_add](https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-idle-add) – AndreLDM May 10 '17 at 14:49
  • 1
    GTK is not thread safe and thus code registered with [idle_add](http://gtk-rs.org/docs/glib/fn.idle_add.html) will be called on the main thread. But code that never calls GTK can run in a thread like you did, so your suggestion is fine. Does this answer your question? – Jan Zerebecki May 10 '17 at 14:59
  • @JanZerebecki Thanks for your comment, after this question, even worked as a new thread, I was looking for yesterday and found `glib::idle_add` As you also comment and I had some errors when trying to use it -> http://stackoverflow.com/questions/43882354/native-library-glib-is-being-linked-to-by-more-than-one-package-and-can-only Maybe I want to have a look. Thanks for your time – Angel Angel May 10 '17 at 15:53
  • @AndreLDM Thanks for your comment And its link, but JanZerebecki, put the rust-rs link, maybe it is more useful for other users but thanks – Angel Angel May 10 '17 at 15:55
  • Just a comment, if you want to share some code, it's probably a better idea to program in english. Function names are in english and it's not a good idea for readability to mix several languages. – liberforce May 11 '17 at 11:17

0 Answers0