Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Freezing issue caused by socket and UI updates in the thread and main thread
Qt 6.11 is out! See what's new in the release blog

Freezing issue caused by socket and UI updates in the thread and main thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 81 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Joe von HabsburgJ Online
    Joe von HabsburgJ Online
    Joe von Habsburg
    wrote last edited by Joe von Habsburg
    #1

    I use UDP socket. This socket runs on a separate thread.

    I receive graphical data through the socket. I then plot the incoming data on a graph.

    QTimer m_processer;
    QElapsedTimer m_sender;
    double m_sendTime = 5;
    double m_parseTime = 1;
    
    UdpSocket::UdpSocket(QObject *parent)
        : QObject{parent}
    {
        connect(&m_processer, &QTimer::timeout, this, &UdpSocket::processIncomingDatagram);
        m_processer.start(m_parseTime);
    
        m_sender.start();
    }
    
    void UdpSocket::readyRead()
    {
        while(m_socket->hasPendingDatagrams()){
            QNetworkDatagram datagram = m_socket->receiveDatagram();
            m_buffer.append(datagram.data());
        }
    }
    
    void UdpSocket::processIncomingDatagram()
    {
        while (true) {
            // Something check here
            parseData(frame);
            m_buffer.remove(0, 8 + static_cast<int>(len));
        }
    }
    
    // MySocket takes inheritance from UdpSocket
    
    void MySocket::parseData(QByteArray data)
    {
        // Parsing here
    
        m_received += count;
    
        if(m_received >= size){
            m_received = 0;
            qint64 elapsed = m_sender.elapsed();
            if(elapsed >= m_sendTime){
                emit sendData(. . .);
                m_sender.restart();
            }
            qDebug() <<  "RECEIVE :" << size << elapsed;
        }
    }
    
    void MainWindow::sendData(. . .)
    {
        if (m_processing)
            return;
    
        m_processing= true;
    
        if(m_qwt){
            m_qwt->compute(. . .);
            m_qwt->render();
        }
    
        m_processing = false;
    }
    

    I mentioned earlier that the MySocket is running in a different thread.

    RECEIVE : 131072 6
    RECEIVE : 131072 6
    RECEIVE : 131072 5
    RECEIVE : 131072 6
    RECEIVE : 131072 6
    RECEIVE : 131072 7
    RECEIVE : 131072 4
    RECEIVE : 131072 10
    RECEIVE : 131072 6
    RECEIVE : 131072 4
    RECEIVE : 131072 10
    RECEIVE : 131072 4

    I am able to get an output like this

    In addition, the QWT update on the main window takes a maximum of 5 ms

    What should I do? What do you recommend? How can I process data that’s coming in this fast?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by SGaist
      #2

      Hi,

      I would:

      • Remove that infinite loop -> Qt is event driven so grab the data when it arrives then trigger the processing
      • If the data is "too fast" then there's no sense in trying to show it all the time, your users won't be able to see all these changes (it's like trying to show a billion points -> useless). Batch the updates at a rate that is useful.

      Also, there's no need to subclass QUdpSocket, you just use it thus avoid subclassing in this case.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Joe von HabsburgJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        I would:

        • Remove that infinite loop -> Qt is event driven so grab the data when it arrives then trigger the processing
        • If the data is "too fast" then there's no sense in trying to show it all the time, your users won't be able to see all these changes (it's like trying to show a billion points -> useless). Batch the updates at a rate that is useful.

        Also, there's no need to subclass QUdpSocket, you just use it thus avoid subclassing in this case.

        Joe von HabsburgJ Online
        Joe von HabsburgJ Online
        Joe von Habsburg
        wrote last edited by Joe von Habsburg
        #3

        @SGaist said in Freezing issue caused by socket and UI updates in the thread and main thread:

        Remove that infinite loop -> Qt is event driven so grab the data when it arrives then trigger the processing

        That for check crc

        void UdpSocket::processIncomingDatagram()
        {
            static const QByteArray magic("\x55\xAA\x55\xAA", 4);
            while (true) {
                const int pos = m_buffer.indexOf(magic);
                if (pos < 0) { m_buffer.clear(); break; }
                if (pos > 0) m_buffer.remove(0, pos);
                if (m_buffer.size() < 8) break;
                const quint32 len = qFromBigEndian<quint32>(reinterpret_cast<const uchar*>(m_buffer.constData() + 4));
                if (m_buffer.size() < 8 + static_cast<int>(len)) break;
                const QByteArray frame = m_buffer.left(8 + static_cast<int>(len));
                if (!calculateCrc(frame)) {
                    m_buffer.remove(0, 1);
                    continue;
                }
                parseData(frame);
                m_buffer.remove(0, 8 + static_cast<int>(len));
            }
        }
        

        @SGaist said in Freezing issue caused by socket and UI updates in the thread and main thread:

        If the data is "too fast" then there's no sense in trying to show it all the time, your users won't be able to see all these changes (it's like trying to show a billion points -> useless). Batch the updates at a rate that is useful.

        I also use decimation for points.

        @SGaist said in Freezing issue caused by socket and UI updates in the thread and main thread:

        Also, there's no need to subclass QUdpSocket, you just use it thus avoid subclassing in this case.

        no no no my UdpSocket class is QObject

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved