Merge branch 'master' into file_dialog_crash_qt59

This commit is contained in:
Vladyslav Stelmakhovskyi 2017-06-28 16:01:18 +02:00
commit 6acf426b1e

View file

@ -1067,24 +1067,21 @@ void ScriptEngine::run() {
// on shutdown and stop... so we want to loop and sleep until we've spent our time in
// purgatory, constantly checking to see if our script was asked to end
bool processedEvents = false;
while (!_isFinished && clock::now() < sleepUntil) {
{
PROFILE_RANGE(script, "processEvents-sleep");
QCoreApplication::processEvents(); // before we sleep again, give events a chance to process
if (!_isFinished) {
PROFILE_RANGE(script, "processEvents-sleep");
std::chrono::milliseconds sleepFor =
std::chrono::duration_cast<std::chrono::milliseconds>(sleepUntil - clock::now());
if (sleepFor > std::chrono::milliseconds(0)) {
QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(sleepFor.count());
loop.exec();
} else {
QCoreApplication::processEvents();
}
processedEvents = true;
// If after processing events, we're past due, exit asap
if (clock::now() >= sleepUntil) {
break;
}
// We only want to sleep a small amount so that any pending events (like timers or invokeMethod events)
// will be able to process quickly.
static const int SMALL_SLEEP_AMOUNT = 100;
auto smallSleepUntil = clock::now() + static_cast<std::chrono::microseconds>(SMALL_SLEEP_AMOUNT);
std::this_thread::sleep_until(smallSleepUntil);
}
PROFILE_RANGE(script, "ScriptMainLoop");