Results 1 to 12 of 12

Thread: GUI thread blocked using QThread.

  1. #1
    Join Date
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default GUI thread blocked using QThread.

    Hi All,

    I have created one class LogTask to store logs in certain file.
    My problem is it's blocking GUI thread when I call Log(QString("")) .

    Here is the code.

    Can anyone help me with this???

    Thanks in Advance
    ---
    Qt Code:
    1. [CENTER][B]Task.cpp[/B][/CENTER]
    2.  
    3. typedef QList<QEvent*> Pocket;
    4. Pocket mypocket;
    5. Task::Task():QThread() //constructor for task
    6. {
    7. taskmanager::addTask(this); //adding task to the tasklist
    8. stopsignal = false;
    9.  
    10. }
    11.  
    12. void Task::run(){
    13. while (!stopsignal)
    14. {
    15. mymutex.lock();
    16. mycondition.wait(&mymutex);
    17.  
    18. // got a signal..now process
    19. processIncomingEvents();
    20. mymutex.unlock();
    21.  
    22. }
    23. }
    24.  
    25. void Task::processIncomingEvents()
    26. {
    27. QEvent* e = mypocket.first();
    28. while (e){
    29. handleEvent(e);
    30. mymutex.lock();
    31. mypocket.remove(e);
    32. mymutex.unlock();
    33. e = mypocket.first();
    34. }
    35. //mypocket.clear();
    36. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. [CENTER][B]LogTask.h[/B][/CENTER]
    2.  
    3. #define FILE_PATH "d:/Aer2.log"
    4. #define Log(x) LogTask::OutputDebugLog(QString(x+" in %1 in %2 at %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__))
    5.  
    6. class LogTask : public Task
    7. {
    8. public:
    9. LogTask();
    10. ~LogTask();
    11.  
    12. bool Initialize();
    13. bool Finalize();
    14. virtual void handleEvent( QEvent* ev );
    15. static void OutputDebugLog( const QString & );
    16.  
    17. };
    18.  
    19. //##################################################
    20.  
    21. [CENTER][B]LogTask.cpp[/B][/CENTER]
    22.  
    23. LogTask* currentlogtask=0;
    24.  
    25. LogTask::LogTask()
    26. {
    27. currentlogtask = this;
    28. }
    29.  
    30. LogTask::~LogTask()
    31. {
    32. qDebug("Logtask ended");
    33. currentlogtask = 0;
    34. }
    35.  
    36. bool LogTask::Initialize()
    37. {
    38.  
    39. /*
    40. file.setName(FILE_NAME);
    41. if (!file.open(IO_WriteOnly | IO_Append))
    42. return -1;
    43. ts.setDevice(&file);*/
    44.  
    45. return true;
    46. }
    47.  
    48. bool LogTask::Finalize()
    49. {
    50. //ts.flush();
    51. //ts.close();
    52. //file.close();
    53. return true;
    54. }
    55.  
    56. void LogTask::OutputDebugLog(const QString &str)
    57. {
    58.  
    59. if (currentlogtask)
    60. currentlogtask->acceptEvent( new HonCustomEvent(ETLOG, 0, 0, new QString(str)));
    61. }
    62.  
    63.  
    64.  
    65. void LogTask::handleEvent( QEvent* ev )
    66. {
    67. HonCustomEvent* rev = (HonCustomEvent*) ev;
    68. if (rev->eventtype == ETLOG)
    69. {
    70. QString path = FILE_PATH;
    71. QString* str = (QString* )rev->ptr;
    72. QFileInfo fileinfo(path);
    73.  
    74. if(fileinfo.size() >= 5000000)
    75. { ///If file is 5MB in size change the file to output everything
    76. QString newpath = QString(FILE_PATH)+"."+"0";
    77. QFileInfo tmpinfo(newpath);
    78. if(tmpinfo.exists())
    79. {
    80. QDir dir = tmpinfo.dir();
    81. dir.remove(newpath);
    82. }
    83. QFileInfo newinfo(path);
    84. QDir newdir = newinfo.dir();
    85. newdir.rename(path,newpath);
    86. }
    87.  
    88. QFile file(path);
    89.  
    90. if(!file.open(IO_WriteOnly | IO_Append))
    91. {
    92. return;
    93. }
    94. QTextStream ts(&file);
    95.  
    96. ts << (*str)+"\n";
    97. file.close();
    98. delete str;
    99. }
    100.  
    101. return;
    102. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. [CENTER][B]taskmanager.cpp[/B][/CENTER]
    2.  
    3. #include "taskmanager.h"
    4.  
    5.  
    6. TaskList tasklist;
    7.  
    8. void taskmanager :: stopAllTasks()
    9. {
    10. Task *t = tasklist.first();
    11. while(t) {
    12. t->stopnow();
    13. tasklist.remove(t);
    14. t = tasklist.first();
    15. }
    16. tasklist.clear();
    17.  
    18. }
    19.  
    20. void taskmanager :: addTask(Task *t)
    21. {
    22.  
    23. // Task *t = tasklist.getlast();
    24.  
    25. //add task to the list
    26. tasklist.append( t );
    27.  
    28. }
    29.  
    30. int taskmanager :: getCountTasks()
    31. {
    32. return tasklist.count();
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GUI thread blocked using QThread.

    Sorry forgot to mention I am invoking this thread in MainWindow constructor.
    Qt Code:
    1. ############Mainwindow.cpp#############
    2. MainWindow::AppMainWindow(QWidget *parent, Qt::WFlags flags)
    3. : QMainWindow(parent, flags)
    4. {
    5. LogTask *log = new LogTask();
    6. log->start();
    7.  
    8. Log(QString("Start Creating Actions\n"));
    9.  
    10. createActions();
    11.  
    12. Log(QString("Start Creating ToolBar\n"));
    13. createToolBar();
    14.  
    15. Log(QString("Start Creating Menus\n"));
    16. createMenus();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in Advance.
    ----

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

    Default Re: GUI thread blocked using QThread.

    You are calling a method on the thread object from the main thread thus it is executed in the context of the main thread and not your logging 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.


  4. #4
    Join Date
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GUI thread blocked using QThread.

    "You are calling a method on the thread object from the main thread"

    I didn't get this.

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GUI thread blocked using QThread.

    try changing your thread's affinity by using moveToThread like this:

    Qt Code:
    1. myOtherThread->moveToThread(myOtherThread);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GUI thread blocked using QThread.

    I don't know but this is very strange problem I am facing.

    It's working perfectily OK with only one Log(QString("")) , problem occurs when I introduces some more Log(QString("")) in different functions.

    E.g.
    If I comment
    Log(QString("Start Creating ToolBar\n"));
    Log(QString("Start Creating Menus\n"));

    Than application will work fine and Log file is also created .

    Is everything OK with QString.???

    I am unable to track the error.

  7. #7
    Join Date
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GUI thread blocked using QThread.

    I don't know but this is very strange problem I am facing.

    It's working perfectily OK with only one Log(QString("")) , problem occurs when I introduces some more Log(QString("")) in different functions.

    E.g. (
    If I comment these two lines
    Log(QString("Start Creating ToolBar\n"));
    Log(QString("Start Creating Menus\n"));

    and leave
    Log(QString("Start Creating Actions\n"));

    Than application will work fine and Log file is also created .)


    Is everything OK with QString.???

    I am unable to track the error.

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

    Default Re: GUI thread blocked using QThread.

    Quote Originally Posted by summer_of_69 View Post
    "You are calling a method on the thread object from the main thread"

    I didn't get this.
    The context of execution is based not on the thread affinity of the object (which is also living in the main thread so this doesn't change anything but for a moment let's assume it does) containing the called method but on the affinity of the object calling the method. So if you are calling the method from your main window, it will be executed in the context of the thread running your main window (which is the main thread) and your gui will get blocked.

    I don't know what your acceptEvent() method does but unless it calls QCoreApplication:ostEvent() you're using a synchronous call which is again done in the context of the calling thread and blocks your gui because it is your gui thread that makes the call.

    Quote Originally Posted by talk2amulya View Post
    try changing your thread's affinity by using moveToThread like this:

    Qt Code:
    1. myOtherThread->moveToThread(myOtherThread);
    To copy to clipboard, switch view to plain text mode 
    This will change exactly nothing, unless of course postEvent() is used by acceptEvent().
    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
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GUI thread blocked using QThread.

    Quote Originally Posted by summer_of_69 View Post
    I don't know but this is very strange problem I am facing.

    It's working perfectily OK with only one Log(QString("")) , problem occurs when I introduces some more Log(QString("")) in different functions.

    E.g. (
    If I comment these two lines
    Log(QString("Start Creating ToolBar\n"));
    Log(QString("Start Creating Menus\n"));

    and leave
    Log(QString("Start Creating Actions\n"));

    Than application will work fine and Log file is also created .)


    Is everything OK with QString.???

    I am unable to track the error.
    But I can't understand why it's working for above condition ,I send just before your mail.

  10. #10
    Join Date
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: GUI thread blocked using QThread.

    But I can't understand why it's working for above condition ,I send just before your mail.

  11. #11
    Join Date
    May 2009
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: GUI thread blocked using QThread.

    Quote Originally Posted by wysota View Post
    The context of execution is based not on the thread affinity of the object (which is also living in the main thread so this doesn't change anything but for a moment let's assume it does) containing the called method but on the affinity of the object calling the method. So if you are calling the method from your main window, it will be executed in the context of the thread running your main window (which is the main thread) and your gui will get blocked.

    I don't know what your acceptEvent() method does but unless it calls QCoreApplication:ostEvent() you're using a synchronous call which is again done in the context of the calling thread and blocks your gui because it is your gui thread that makes the call.



    This will change exactly nothing, unless of course postEvent() is used by acceptEvent().
    Thanks for your reply .
    This is what my acceptEvent() does.Still I am facing the same problem don't know from where to call my LogTask .

    Qt Code:
    1. Task::Task():QThread() //constructor for task
    2. {
    3. taskmanager::addTask(this); //adding task to the tasklist
    4. stopsignal = false;
    5.  
    6. }
    7.  
    8. void Task::run(){
    9. while (!stopsignal)
    10. {
    11. mymutex.lock();
    12. mycondition.wait(&mymutex);
    13.  
    14. // got a signal..now process
    15. processIncomingEvents();
    16. mymutex.unlock();
    17.  
    18. }
    19. }
    20.  
    21. void Task::processIncomingEvents()
    22. {
    23. QEvent* e = mypocket.first();
    24. while (e){
    25. handleEvent(e);
    26. mymutex.lock();
    27. mypocket.remove(e);
    28. mymutex.unlock();
    29. e = mypocket.first();
    30. }
    31. //mypocket.clear();
    32. }
    33.  
    34. [I]void Task::acceptEvent(QEvent* ev){
    35. mymutex.lock();
    36. mypocket.append(ev);
    37. mymutex.unlock();
    38. mycondition.wakeAll();
    39. }[/I]
    40. ;
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: GUI thread blocked using QThread.

    I'm not sure why you are making things so complex, try this:
    Qt Code:
    1. class LogEvent : public QEvent {
    2. public:
    3. LogEvent(const QString &str) : QEvent(QEvent::MaxUser-1), m_str(str){}
    4. const QString &string() const { return m_str; }
    5. private:
    6. QString m_str;
    7. };
    8.  
    9. class Log : public QThread {
    10. public:
    11. Log(){}
    12. void run(){ exec(); }
    13. static void log(const QString &str){ QCoreApplication::postEvent(new LogEvent(str)); }
    14. protected:
    15. void customEvent(QEvent *e){
    16. if(e->type()==QEvent::MaxUser-1){
    17. logData(static_cast<LogEvent*>(e)->string());
    18. }
    19. }
    20. };
    To copy to clipboard, switch view to plain text mode 

    No need for any mutexes or artificial event queues.
    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.


Similar Threads

  1. QThread event loop seems blocked
    By eurodatar in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2009, 16:50
  2. Replies: 4
    Last Post: 26th June 2008, 18:41
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 06:27
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.