Results 1 to 9 of 9

Thread: UI hangs when either QThread or QTimer invoked

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default UI hangs when either QThread or QTimer invoked

    My program continuously reads from serial port. Firstly, I used QTimer and set the interval to 300 mlscnd, this makes UI interaction very slow. I though of converting QTimer to QThread, as QTimer runs in the UI Thread, so, I added QThread class and used msleep() inside the while() loop to control the trigger.
    Unfortunately, the program is still slow, I can clearly understand this when I enter digits in the UI. My last guess of where the speed reduction might have come from is emit, as you see below:

    Qt Code:
    1. void PlcTimer::run(){
    2. while(1)
    3. {
    4. qDebug() << "updateJack()";
    5.  
    6. const char str[]={UPDATE_m};
    7. #if DEBUG
    8. QByteArray built(str, 3);
    9. qDebug() << built.toHex();
    10. #endif
    11. emit m_sendToRS((char* )str, 3);
    12. this->msleep(500);
    13. //sleep(300);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    emit is a UI object, so whatever it is I am suspicious of its interconnection over the UI. do you have any idea how I can resolve the low speed?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UI hangs when either QThread or QTimer invoked

    First of all emitting a pointer to a local variable to a different thread is not safe. Second of all, show how you use the thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: UI hangs when either QThread or QTimer invoked

    in the MainWindow, I use a new instance of the Object, new PlcTimer() and connect the it to the serial SLOT, then I call the start() me

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UI hangs when either QThread or QTimer invoked

    Show relevant code please. What is the signal connected to?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: UI hangs when either QThread or QTimer invoked

    inside MainWindow:
    Qt Code:
    1. plcTimer = new PlcTimer();
    2. connect(plcTimer, SIGNAL(jack_sendToRS(char*,int)), rs_plc, SLOT(rs_plcDataAqustn(char*,int)));
    3. plcTimer->start();
    4. rs_plc->rs_plcOpenPort((char *)"/dev/ttyS0"); /*/dev/ttyS3*/
    To copy to clipboard, switch view to plain text mode 

    QThread signal or QTimer signal connects to serial SLOT which write n reads packet to and from serial port:
    Qt Code:
    1. bool RS::rs_plcDataAqustn(char* data, int len)
    2. {
    3. QByteArray rd15Bytes;
    4.  
    5. // QByteArray built((char*)data, 6) ;
    6. // qDebug() << built.toHex();
    7.  
    8.  
    9. if(!rs_serialWrite(data, len))
    10. {
    11. qDebug() << "Failure:( rs_dataqustn: rs_plcWrite(data, len)";
    12. emit plc_port_dscntd();
    13. return false;
    14. }
    15.  
    16. if(len == 3)
    17. {
    18. qDebug() << "len = 3";
    19.  
    20. rs_delay();
    21.  
    22. if(rs_plcRead(&rd15Bytes))
    23. {
    24. qDebug() << rd15Bytes.length();
    25. //qDebug() << "->" << rd15Bytes.toHex();
    26.  
    27. if(rd15Bytes.isEmpty())
    28. {
    29. emit logMessage("Failure:( rs_dataAqustn, if(rd15Bytes.isEmpty())");
    30. }
    31. else
    32. {
    33. if(!rs_plcCheckChecksum(&rd15Bytes))
    34. emit logMessage("Failure:( rs_dataAqustn, if(!rs_plcCheckChecksum(&rd15Bytes))");
    35. else
    36. {
    37. emit rs_plcSendRdPacket(rd15Bytes);
    38.  
    39. if(!rs_serialWrite((char* )"0x06", 1))
    40. {
    41. qDebug() << "Failure:( rs_dataqustn: rs_plcWrite(PLC_ACK, 1)";
    42. }
    43. }
    44. }
    45. }
    46. else
    47. {
    48. emit plc_hmiBoard_dscntd();
    49. }
    50. }
    51.  
    52. return true;
    53. }
    To copy to clipboard, switch view to plain text mode 


    Read from Plc:
    Qt Code:
    1. bool RS::rs_plcRead(QByteArray *data)
    2. {
    3. unsigned char buff[128];
    4. int len, tries;
    5.  
    6. tries = 0;
    7.  
    8. while(tries < 13)
    9. {
    10. len = read(fd, buff, PLC_READ_BYTE);
    11.  
    12. if((buff[0] == PLC_HEADER) && (len == PLC_READ_BYTE))
    13. {
    14. data->append((char *)buff, PLC_READ_BYTE);
    15. //qDebug() << data->toHex();
    16. return true;
    17. }
    18. else
    19. {
    20. tries++;
    21. qDebug() << "retry";
    22. }
    23. }
    24.  
    25. return false;
    26. }
    To copy to clipboard, switch view to plain text mode 

    should you need more info, please note it.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UI hangs when either QThread or QTimer invoked

    You really have to provide more code or better yet a minimal compilable example reproducing the problem, I'm not a seer. Currently I'd have to ask about each and every vaguely named variable in your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: UI hangs when either QThread or QTimer invoked

    WYSOTA!
    I found out what was lowering down system speed. emit n signals are handled by UI, the main program Thread. Connecting object to serial port class as you see above is not a good idea, as these all are handled by UI Thread. Hence, reducing UI interaction speed. I changed Serial class slot to a static function and called the function directly without using emit. Now, two threads are running every 300 milscnd in the background, without using emit and without conflicting UI user interaction!

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UI hangs when either QThread or QTimer invoked

    Emitting signals doesn't slow down things. If it did in your case then your code had to be incorrect.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: UI hangs when either QThread or QTimer invoked

    saman_artorious,

    Why to you not to use ready libraries for operation with the serial ports and do not bother QThread and QTimer?
    Or you have any weighty reasons to use the himself serial port implementation with such perversions?

Similar Threads

  1. Replies: 15
    Last Post: 4th August 2012, 19:11
  2. QTimer within QThread
    By Eos Pengwern in forum Qt Programming
    Replies: 6
    Last Post: 23rd February 2011, 20:00
  3. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  4. QThread & QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 5
    Last Post: 13th April 2009, 08:22
  5. QTimer and QThread
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 14:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.