0

In my (Qt-)program I need a continuous request of a value which I get from an external source. But I did not want that this request freezes the whole program, so I created a separate thread for this function. But even if it is running in a separate thread, the GUI freezes, too. Why?

Code for the request function:

void DPC::run()
{
    int counts = 0, old_counts = 0;
    while(1)
    {
        usleep(50000);
        counts = Read_DPC();
        if(counts != old_counts)
        {
            emit currentCount(counts);
            old_counts = counts;
        }

    }
}

Read_DPC() returns an int value I want to sent to a lineEdit in my GUI.
The main class looks like

class DPC: public QThread
{
    Q_OBJECT
public:
    void run();
signals:
    void currentCount(int);
};

This code is called in the main function as:

DPC *newDPC = new DPC;
connect(newDPC, SIGNAL(currentCount(int)), SLOT(oncurrentCount(int)));
connect(newDPC, SIGNAL(finished()), newDPC, SLOT(deleteLater()));
newDPC->run();

How can I prevent this code from freezing my GUI? What am I doing wrong? Thanks!

arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • How exactly are you launching a separate thread? – Anton Savin Oct 15 '14 at 17:39
  • I thought via creating a subclass which is derived from QThread? According to some threads (http://stackoverflow.com/questions/14545961/modify-qt-gui-from-background-worker-thread) or (http://stackoverflow.com/questions/16501284/qt-updating-main-window-with-second-thread) – arc_lupus Oct 15 '14 at 17:40
  • 2
    Why do you call `run` from the main thread? Didn't you want some other thread to call `run`? Wasn't that the whole point of deriving from QThread? – David Schwartz Oct 15 '14 at 17:51
  • How should I start the subthread if not using `run`? I tried using `start`, but that did not work, too (I simply replaced `run` with `start`, if that was correct) – arc_lupus Oct 15 '14 at 17:52
  • @arc_lupus One bug at a time. Fix the call to `run` and then re-test, describing in as much detail as possible what goes wrong after that bug is fixed. – David Schwartz Oct 15 '14 at 18:57
  • @DavidSchwartz: Thanks for the comment, but my problem is already solved... – arc_lupus Oct 15 '14 at 19:11
  • This is probably one of the reasons why `QThread::run` is protected. You should make it a protected member as well. – thuga Oct 16 '14 at 07:14

1 Answers1

5

It seems that you code run in GUI thread because you use run() method to start thread, so try to call start() as documentation and many examples said.

Try:

DPC *newDPC = new DPC;
connect(newDPC, SIGNAL(currentCount(int)), SLOT(oncurrentCount(int)));
connect(newDPC, SIGNAL(finished()), newDPC, SLOT(deleteLater()));
newDPC->start();//not run

Anyways you can call thread() method or currentThread() to see in which thread some objects live.

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Using `start()` leads to freeze, too. – arc_lupus Oct 15 '14 at 17:49
  • @arc_lupus add qDebug() << thread() to your mainWindow and in the run() Did you get same threads? What exactly you get? Same threads means same adress in the output – Jablonski Oct 15 '14 at 17:51
  • @arc_lupus I run same code right now and try to undestand problem. Tell me please what you can see when in run you'll wrote(in while loop): `qDebug() << thread() << currentThread();` – Jablonski Oct 15 '14 at 18:08
  • @arc_lupus when I use run instead of start my GUI freeze too, call run is wrong, now I try understand why start() do not work for you. – Jablonski Oct 15 '14 at 18:10
  • @arc_lupus and also try `qDebug() << currentThread() <<"constructor";` in thread constructor. – Jablonski Oct 15 '14 at 18:13
  • @arc_lupus currentThread() you should use in QThread subclass because it is a static method of QThread – Jablonski Oct 15 '14 at 18:16
  • just assumption, will you have the same freezing if you will comment out usleep(50000); ? – Max Go Oct 15 '14 at 18:16
  • @Chernobyl: Output in main thread is: QThread(0x76aad0) QThread(0x76aad0), in sub thread I get QThread(0x1f80ad0) DPC(0x21aa2d0) – arc_lupus Oct 15 '14 at 18:18
  • @N1ghtLight no, I just get more then 271374 prints, because while(1) is very fast :) – Jablonski Oct 15 '14 at 18:19
  • @Chernobyl, I though that usleep is unix native method, but it looks like here it's QThread's method – Max Go Oct 15 '14 at 18:20
  • @Chernobyl: Freeze is gone if I remove the usleep(), but I get no update of the lineEdit() – arc_lupus Oct 15 '14 at 18:20
  • @arc_lupus Something strange. I'm on Windows and usleep works normal for me, maybe try to use msleep? Set interval in milliseconds. – Jablonski Oct 15 '14 at 18:23
  • @Chernobyl: That works, too, but I still get no signal – arc_lupus Oct 15 '14 at 18:26
  • @arc_lupus Please clarify, you GUI now not freeze and your thread works normal(print something every time to see that all good)but you can't catch signal, then try to print something in slot too(to know is slot was called at all). And post code of slot – Jablonski Oct 15 '14 at 18:35
  • @Chernobyl: Problem solved, my read_DPC() function simply does not return anything-> nothing to display... – arc_lupus Oct 15 '14 at 18:40
  • @arc_lupus Ok, we do this :) – Jablonski Oct 15 '14 at 18:47
  • 1
    @N1ghtLight I will, it was cool.There is no any result from voting up comment, but your contribution will not be forgotten or omitted ;) – Jablonski Oct 15 '14 at 18:47