How to deal: GUI is not freezed but doesn’t work

  c++, client-server, multithreading, qt, tcp

I have a client-server system implemented using QTcpServer. In paticulary, client does some stuff like

open file => read 2k lines => send it to the server

I can just say, that the file is extremly big (200 000 rows).

The problem:
I do this (opening the file, reading, …, sending) from different thread, cos i would like to have a working statusbar. But it doesn’t work. At the begining it calculates a bit, but then stops (I can interact with gui! It is not freezed!).

Some code:

  1. Creating thread and class. Then moveToThread:
        // THREAD
        thread = new QThread;
        thread->setPriority(QThread::NormalPriority); // tried to fix this issue using this

        // WORKER
        this->RAS = new ReaderAndSender(...);
        RAS->moveToThread(thread);

        connect(RAS, SIGNAL(countChanged(int)), this, SLOT(updateProcessInfo(int))); // <---- that's it

        connect(thread, SIGNAL(started()), RAS, SLOT(doWork()));
        connect(RAS, SIGNAL(errorDetected()), this, SLOT(handleError()));
        
        // clearing after myself
        connect(RAS, SIGNAL(finished()), thread, SLOT(quit()));
        connect(RAS, SIGNAL(finished()), RAS, SLOT(deleteLater()));
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

        thread->start();
  1. doWork() and some another stuff
   void doWork()
   {
       ...

       while(!in.atEnd())
       {
           ...
           if(count % 7 == 1) // random condition
               emit countChanged(count);
           
           if(...) // if count==2k or end of file
           {
               // SEND DATA
               if(client->send_and_get_response(...) == "0")
               {
                   emit errorDetected();
                   break;
               }
               ...
           }
       }
       emit finished();
   };

signals:
   void countChanged(int count);
   void errorDetected();
   void finished();
...

Source: Windows Questions C++

LEAVE A COMMENT