Results 1 to 6 of 6

Thread: Blocking main thread, How?

  1. #1
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Blocking main thread, How?

    Hi all,
    What are ways for blocking main thread?? I want to check for database connection in main thread before showing anything (e.g. mainwindow). There is some limitations:
    I have a qml file which i show it before mainwindow and actually is my splash screen, i want to show database connection status on it so while blocking main thread, it have to be active and not blocked.
    while blocking i want to run a thread to check database connection in interval ( the splash screen (qml file) is showing the progress).

    the main.cpp is something like this:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. //-------------------------Load-SPLASH
    5. QQuickView splash;
    6. splash.setSource(QUrl("qrc:/qml/TSplashScreen.qml"));
    7. splash.show();
    8. app.processEvents();
    9.  
    10. //Check if database is not connected
    11. TInitialDBConnectionCheck initializeDBCheck;
    12. if(!initializeDBCheck.isDBConnected())
    13. {
    14. //Here i have to block main thread and start a thread to check database status intevally. i know how to deal with second but i don't know how to block main thread to stay here while database is ready.
    15. }
    16.  
    17. MainWindow mw;
    18. splash.close();
    19. mw.showFullScreen();
    20.  
    21.  
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    In addition there is a method called exec() in dialog that do whatever i want !!! but i have here a qml file and a mainthread and another custom thread!
    Last edited by alizadeh91; 14th October 2012 at 14:38.

  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: Blocking main thread, How?

    QMutex, QWaitCondition. But then ask yourself whether you need it at all. Why not just move your extra thread logic into the main 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. The following user says thank you to wysota for this useful post:

    alizadeh91 (14th October 2012)

  4. #3
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Blocking main thread, How?

    Thanks,
    I want to check database connection for every one second(if is disconnected) until connection established. So it seems it have to be done in a thread. That's right?

  5. #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: Blocking main thread, How?

    No, what does it change to have a thread here?
    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.


  6. #5
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Blocking main thread, How?

    so how to use a loop for every second in main thread?

  7. #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: Blocking main thread, How?

    It depends whether you can afford running an event loop or not. If yes, then use a timer, if no then use sleep().
    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. Replies: 5
    Last Post: 22nd June 2012, 16:40
  2. PyQT, QThread, thread blocking
    By Cirno in forum Newbie
    Replies: 0
    Last Post: 26th December 2011, 01:57
  3. Replies: 15
    Last Post: 24th December 2011, 12:07
  4. Non-Gui thread blocking gui
    By Valheru in forum Qt Programming
    Replies: 17
    Last Post: 12th February 2010, 11:11
  5. Best thread non-blocking technics
    By Tanuki-no Torigava in forum Qt Programming
    Replies: 0
    Last Post: 8th December 2009, 15:53

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.