MessageDialog在单独的线程中
MessageDialog in separate thread
在我当前的项目中,我需要在按下特定按钮时执行一些计算,当我执行这些计算时,我想显示一个Gtk::MessageDialog
,它简单地声明正在执行计算。因此,我像这样初始化MessageDialog(暂时忽略这里实际上不需要指针):
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;
接下来我想用对话框启动一个单独的线程:
std::thread dialog_thread(wait_dialog,waitdialog);
wait_dialog方法定义如下:
void wait_dialog(Gtk::MessageDialog *dialog){
dialog->run();
}
现在的问题是,即使主窗口变暗(因为set_transient_for),消息对话框是不可见的。然而,当我不启动一个单独的线程,而只是调用waitdialog->run()
代替,这将显示对话框正确(但将导致一个循环)。
那么,问题是:为什么与单独的线程的解决方案不工作?我看不懂-(
需要GUI组件留在GUI循环中。长时间运行的计算属于线程。计算线程然后返回信号给GUI线程关闭模态对话框。此外,您应该使用glib线程而不是std::线程。下面是我将如何构建程序:
// in header, member var dispatcher used to signal GUI thread
// and our member var thread
Glib::Dispatcher m_signalDone;
Glib::Thread* m_someThread;
...
// in constructor, hook up dispatcher event
m_signalDone.connect(sigc::mem_fun(this, &MyClass::OnDone));
...
// later when ready to kick off thread...
// show dialog or progess bar or something and kick off thread...
m_someThread = Glib::Thread::create(sigc::mem_fun(*this, &MyClass::CalcMethod), true);
...
void MyClass::CalcMethod()
{
// do your long running stuff...
// when done signal completion back to GUI
m_signalDone.emit();
}
...
void MyClass::OnDone()
{
// clean up dialog or progress bar or whatever
// kill thread
m_currentBackUpThread->join();
m_currentBackUpThread = NULL;
}
相关文章:
- CoInitialize()在单独的线程上崩溃而不返回
- 自 Windows 10 20H1 以来,具有单独线程的多个窗口停止工作
- 如何从单独的线程控制 SFML 窗口?
- 如何检索指向在单独线程上运行的函数的移动指针?
- 是否可以在单独的线程中将 QObject 设置为 QML 上下文属性?
- QT访问表小部件从单独的线程项目
- Qt - 在单独的线程中运行函数
- 如何在单独的线程上运行函数(如果线程可用)
- 多个boost io_service在单独的线程上用于多个网卡
- 在 Linux 中从单独的单线程进程生成多线程进程时出现问题
- 在单独的线程中使用参数启动成员函数
- 如何将数据写入 stdin,以便由等待来自 stdin 输入的单独线程使用?
- 从 Visual C++ 2010 中的单独线程调用消息框
- XAudio2 教程 - 单独的线程和异步读取?
- 它是否需要为webrtc本机应用程序中的每个对等体提供单独的rtc::Runnable线程和PeerConnection
- UWP getAsynckeystate()来自单独的线程
- QTimer对象是否在单独的线程中运行?它的机制是什么
- 如何在单独的线程上加载纹理到主内存并使用它在另一个线程上渲染
- 标准C++11是否保证std::async(std::launch::asyncfunc)在单独的线程中启动func
- 为什么我的变量在单独的线程中修改时没有改变?C++/boost