0

This is what I have:

void MyThread::run() {
  int progress = 0;
  while (1) {
    // do something
    progres++;
    emit(progressChanged(progress));
  }
}

// mainwindow
auto t = new MyTread();
connect(t, SIGNAL(progressChanged(int)), this, SLOT(onProgressChanged(int)));
t->start();

void MaiWindow::onProgressChanged(int) {
  this->progressBar->setValue(progressBar->value() + 1);
}

It works, the job in the thread gets done, and the progress bar goes all the way up to 100%.

But the UI is completely frozen/laggy. Dragging a window with the progress bar results in a 5 second delay. I tried using lower thread priorities - no result.

Maybe I need a mutex here?

Alex
  • 34,581
  • 26
  • 91
  • 135
  • Is the UI actually frozen, or just slower/very slow? – Brady Jun 14 '12 at 18:31
  • It looks like its most likely because of the "do something" in the thread while loop. What is happening there? – Brady Jun 14 '12 at 18:33
  • 5
    How many progressChanged signals are you emitting per second? Signals are fast, but if you are setting progress bar value hundreds or thousands times per second, the UI will freeze. Keep the progress bar changes to minimum, 5-10 changes per second is more than enough. –  Jun 14 '12 at 18:51
  • @Roku, you are right, it's emitted about 18 000 times per second. Didn't really think about that. I decreased that to 10 times per second, and now there's no freezing. Moreover, the process itself is several times faster. Thanks a lot! Please post an answer so I can accept it. – Alex Jun 14 '12 at 20:59

1 Answers1

3

Do not emit too many progressChanged signals. Signals are fast, but if you are setting progress bar value hundreds or thousands times per second, the UI will freeze. Keep the progress bar changes to minimum, 5-10 changes per second is more than enough.

  • +1 This is one of those, (very few), thread operations where I recommend polling from a Forms timer, eg. like BitTorrent clients - if all those progress pars and percentages were updated on every message with all the peers, the client bould blow up. – Martin James Jun 15 '12 at 12:53