c++ - MessageDialog in separate thread -
in current project need perform calculations when specific button pressed, , while perform these calculations, want show gtk::messagedialog states calculations beingness performed. so, initialize messagedialog (for moment ignore don't need pointer here):
gtk::messagedialog *waitdialog; gtk::messagedialog dia("processing", false, gtk::message_info, gtk::buttons_none, true); dia.set_title("wait."); dia.set_transient_for(*(gtk::window *)this); waitdialog = &dia; next want start separate thread dialog:
std::thread dialog_thread(wait_dialog,waitdialog); the wait_dialog method defined follows:
void wait_dialog(gtk::messagedialog *dialog){ dialog->run(); } the problem is, though main window darkened (because of set_transient_for), message dialog not visible. however, when don't start seperate thread, phone call waitdialog->run() instead, show dialog (but result in loop).
so, question is: why workaround separate thread not work? can't create sense of :-(
gui components are required remain in gui loop. long running calculations belong in thread. calculation thread signals gui thread close modal dialog. also, should utilize glib threads instead of std::threads. here's how construction program:
// in header, fellow member var dispatcher used signal gui thread // , our fellow member var thread glib::dispatcher m_signaldone; glib::thread* m_somethread; ... // in constructor, hook dispatcher event m_signaldone.connect(sigc::mem_fun(this, &myclass::ondone)); ... // later when ready kick off thread... // show dialog or progess bar or , kick off thread... m_somethread = glib::thread::create(sigc::mem_fun(*this, &myclass::calcmethod), true); ... void myclass::calcmethod() { // long running stuff... // when done signal completion gui m_signaldone.emit(); } ... void myclass::ondone() { // clean dialog or progress bar or whatever // kill thread m_currentbackupthread->join(); m_currentbackupthread = null; } c++ multithreading gtkmm
No comments:
Post a Comment