1

I'm using QtConcurrent::run to execute some functions in background and not hang the GUI thread. In one function, I read logs from local SQlite database and send them to server by TCP socket.

Now I want to delay the execution after each log so the server has time to save it (TCP response is read in different thread). I'm stuck with Qt4.8 due to implementation limitations (many embeded devices - no chance to upgrade QT on them) and I can't use QThread::sleep(2) because it is protected in 4.8. Is it possible to somehow pause the execution of thread inside QtConcurrent::run method or should I redesign it to implement my own class inheriting QThread?

void MainWindow::ReportFinishedHUs(bool asyncWork)
{
if(asyncWork == false)
{
    QMutexLocker locker(&localDBmutex);
    QList<QSqlRecord> HUsToReport = localDB->getHUsForBook();
    qDebug() << "HUs to report" << HUsToReport.count();
    if(!HUsToReport.isEmpty())
    {
        Cls_log::WriteDebugLog("HUs to report: " + QString::number(HUsToReport.count()));
        foreach (QSqlRecord record, HUsToReport)
        {
            int _hu = record.indexOf("hu");
            int _logTime = record.indexOf("logTime");

            QString logTimeString = record.value(_logTime).toString();
            QString hu = record.value(_hu).toString();
            qDebug() << hu << logTimeString;

            // creating message here ...

            qDebug() << message;
            emit sig_SendTCPMessage(message);

            // this is where I need to wait for 2 seconds
            QThread::sleep(2);
        }
    }
}
else
{
    QtConcurrent::run(this, &MainWindow::ReportFinishedHUs, false);
}
}

EDIT:

Solved by usleep(2000000) which I somehow discarded for being platform specific... but hey, half of my aplication is platform specific and I only use it in embeded device with constant OS.

Keeping the question open if anyone can suggest more elegand solution using Qt methods. I like to get inspired.

DangeMask
  • 531
  • 4
  • 19
  • What about [`std::this_thread::sleep_for()`](https://en.cppreference.com/w/cpp/thread/sleep_for)? – Scheff's Cat Mar 11 '19 at 12:03
  • "Now I want to delay the execution after each log so the server has time to save it" So wait for the server to reply - or have the server tell the client to pause and restart. Your 2 second wait could be very wrong if the server is struggling and needs 20 seconds to process the message. – UKMonkey Mar 11 '19 at 12:17
  • @Scheff : dind't work out for me, I wasn't able to implement std chronoliterals without upgrading C++ on our embeded device. But thank you for your suggestion. – DangeMask Mar 12 '19 at 10:47
  • @UKMonkey : That would indeed be the most elegant way, but that would also generate the need to reimplement the whole TCP communication. This function is not so critical to pass the data on first try, also the server application keeps queue of requests from each device, so maybe I even don't need the pause. I only assumed it would be better to not throw all logs at the server at once. – DangeMask Mar 12 '19 at 10:53
  • @DangeMask "reimplement the whole TCP communication" why is that? you just broadcast a single item rather than lots - and then on a reply call the same function again ... then when there's no item's left, the function ends up just doing nothing. The alternative would be to just write it all, and let the TCP protocol handle the buffering... but you would have to queue items in a way that could deal with TCP write failures. I'd hope you could deal with them anyway. – UKMonkey Mar 12 '19 at 12:31
  • @UKMonkey in reality, there should be only one log to write (calling this function each 30s and they geerate one log in 5 minutes. All this buffering is in case of network failure or something wrong with the log (server can return error that this line cannot be processed. If I would call the function again, I would read the same line (first not processed) again and again or I would have to remember that I already tried this line and somehow return to it after all other were logged or tried to log... This way I try to log each line on each function call. – DangeMask Mar 12 '19 at 12:48

0 Answers0