PDA

View Full Version : Blocking main thread, How?



alizadeh91
14th October 2012, 14:31
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:




int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//-------------------------Load-SPLASH
QQuickView splash;
splash.setSource(QUrl("qrc:/qml/TSplashScreen.qml"));
splash.show();
app.processEvents();

//Check if database is not connected
TInitialDBConnectionCheck initializeDBCheck;
if(!initializeDBCheck.isDBConnected())
{
//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.
}

MainWindow mw;
splash.close();
mw.showFullScreen();


return app.exec();
}



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!

wysota
14th October 2012, 14:39
QMutex, QWaitCondition. But then ask yourself whether you need it at all. Why not just move your extra thread logic into the main thread?

alizadeh91
14th October 2012, 15:11
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?

wysota
14th October 2012, 16:51
No, what does it change to have a thread here?

alizadeh91
14th October 2012, 18:16
so how to use a loop for every second in main thread?

wysota
14th October 2012, 20:09
It depends whether you can afford running an event loop or not. If yes, then use a timer, if no then use sleep().