Yet another problem with threads :<



I'm trying to utilize threads in my app. Since there aren't many materials
covering this topic this may be something trivial, but I really searched a
lot and didn't find anything that would help me.
This is what I've achieved so far :
/****************  hellothread.h  **************/
class hellothread : public Gtk::Window
{
    public:
        hellothread();
        virtual ~hellothread();
        void on_button_clicked();
        void thread_dilate();
    private:
        Gtk::Button _mButton;
        Glib::Dispatcher m_signal_finished;
};
/*************  hellothread.cpp  *****************/
hellothread::hellothread()
: _mButton("Hello World")
{
    _mButton.signal_clicked().connect(sigc::mem_fun(*this,
&hellothread::on_button_clicked));
    add(_mButton);
    _mButton.show();
}

hellothread::~hellothread()
{
    //dtor
}

void hellothread::on_button_clicked()
{
    Glib::Thread * thread = Glib::Thread::create(
sigc::mem_fun(*this,&hellothread::thread_dilate), true);
    thread->join();
}

void hellothread::thread_dilate()
{
    _mButton.set_sensitive(false);
    _mButton.set_label("Sleeping...");
    std::cout << "sleeping" << std::endl;
    sleep(1);
    std::cout << "finished sleeping" << std::endl;
    _mButton.set_label("Hello World");
    _mButton.set_sensitive(true);
    m_signal_finished.emit();
}
/*************** main.cpp ***************/
int main(int argc, char* argv[])
{
	Gtk::Main kit(argc, argv);


    hellothread thread_;
    Glib::thread_init();
    thread_.set_title( "Thread" );
    Gtk::Main::run(thread_); //Shows the window and returns when it is
closed.

  return 0;
}
/******************************/

App is working, but doesn't seem to use threads. The button's caption isn't
altered and app just "hangs" for one second.
If anyone would like to help me I'd be very grateful.
Best regards,
Bolek.
-- 
View this message in context: http://www.nabble.com/Yet-another-problem-with-threads-%3A%3C-tf3193127.html#a8864770
Sent from the Gtkmm mailing list archive at Nabble.com.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]