PDA

View Full Version : Problem pausing the main thread



franco.amato
11th May 2011, 20:02
Hi to all,
I would pause the main thread for 2-3 sec and then exit from the application but I can not find a solution.

In my case when application starts a status dialog appears showing some result and then the main window is shown. There is a case where I would wait a couple of seconds ( to give to the user the time to read ) and then exit completely from the application. This is the code I wrote:


void CertiIrisApp::init() //CertiIrisApp is the main application derived class
{
start = new StartDlg(0); // start is a dialog showing some initialization results
start->show();
start->print(QString("> Welcome to CertiIris")); //print prints a message
processEvents();
start->print(QString("> Checking for Iris scanner..."));
if (checkIrisScanner())
{
start->print(QString("> Correct Iris scanner connected."));
processEvents();
}
else
{
QString msg = "> An incorrect iris scanner has been connected, contact author.";
start->print(msg);
processEvents();
Sleep(1); // I would wait to give the time to read
QCoreApplication::exit(1); //and then exit
}

if ( connectToDB() == true )
{
QString str = "> " + QCoreApplication::applicationName() + " is successfully connected to the IrisServer.";
start->print(str);
processEvents();
onLoadDBRecords();
}
else
{
QString str = "> " + QCoreApplication::applicationName() + " could not connected to the IrisServer.";
start->print(str);
processEvents();
}

start->hide(); // hide the dialog
delete start; // and delete it
m_mainWnd->show(); // here I show the main window
return;
}
The problem of my code is that the program flow doesn't wait at the Sleep call and follow showing the next message.
Where is my mistake?

Regards,
Franco

mvuori
11th May 2011, 21:44
Are you sure your Sleep() isn't a Windows API call in which case it sleeps only for one millisecond?

franco.amato
11th May 2011, 22:19
Are you sure your Sleep() isn't a Windows API call in which case it sleeps only for one millisecond?

Sorry I did a mistake writing the post.
The right code is Sleep(2000) but I don't know why I see the messages following the call to QCoreApplication::exit(1);
Before exiting the application the program execute some instructions that it shouldn't execute

Lykurg
11th May 2011, 22:47
Is reading the documentation so hard?
Note that unlike the C library function of the same name, this function does return to the caller -- it is event processing that stops.
So simple do:
QCoreApplication::exit(1);
return;