我有一個對話框顯示進度條+其他一些數據,我也有這個對話框上的取消按鈕。在顯示此對話框時,可能會有大量計算正在進行,這顯示在進度條上。這種計算是從withing這個對話框代碼開始,所以我有:終止線程
Counting_Progress_Dialog::Counting_Progress_Dialog(QWidget *parent) :
QDialog(parent)
{
setupUi(this);
thread_ = new Threaded;//THIS IS THE THREAD IN WHICH COMPUTATION IS BEING PERFORMED
connect(thread_,SIGNAL(counter_value(int)),this,SLOT(update_progress_bar(int)));
connect(this,SIGNAL(rejected()),thread_,SLOT(terminate()),Qt::QueuedConnection);//
HERE I'M CONNECTING REJECTED ON DIALOG TO TERMINATE ON THREAD
}
void Counting_Progress_Dialog::start()
{
thread_->start(QThread::LowestPriority);
}
,我做的計劃的一部分調用此:
void My_Class::dummy_()
{
auto old_priority = this->thread()->priority();
this->thread()->setPriority(QThread::HighestPriority);
Counting_Progress_Dialog progress;
progress.start();//this will start thread
progress.exec();//this will enter it's event loop
progress.wait();//this will wait until thread is finished
this->thread()->setPriority(QThread::NormalPriority);
}
但是,儘管這一切,當我按下取消對我的對話,整個應用程序凍結。我究竟做錯了什麼?如何使其行爲正確?
更新:
void Counting_Progress_Dialog::wait()
{
thread_->wait();
}
1.您不應該設置優先級。這只是要求麻煩。別那樣做! 2.你如何處理取消操作? – 2012-03-25 13:06:58
@DavidHeffernan我的取消操作顯示爲最後一個連接 – smallB 2012-03-25 13:10:40
從另一個線程終止線程是不安全和不好的做法。更好地設置「取消」標誌,讓線程的代碼檢查標誌並自行終止它的設置。 – 2012-03-25 16:52:02