diff --git a/src/app/core/vapplication.cpp b/src/app/core/vapplication.cpp index 57a6feb96..b6b3bc84b 100644 --- a/src/app/core/vapplication.cpp +++ b/src/app/core/vapplication.cpp @@ -339,6 +339,43 @@ double VApplication::fromPixel(double pix) const return fromPixel(pix, _patternUnit); } +//--------------------------------------------------------------------------------------------------------------------- +bool VApplication::TryLock(QLockFile *lock) +{ + if (lock == nullptr) + { + return false; + } + + if (lock->tryLock()) + { + return true; + } + else + { + if (lock->error() == QLockFile::LockFailedError) + { + // This happens if a stale lock file exists and another process uses that PID. + // Try removing the stale file, which will fail if a real process is holding a + // file-level lock. A false error is more problematic than not locking properly + // on corner-case systems. + if (lock->removeStaleLockFile() == false || lock->tryLock() == false) + { + return false; + } + else + { + return true; + } + } + else + { + return false; + } + return false; + } +} + //--------------------------------------------------------------------------------------------------------------------- QString VApplication::pathToTables() const { @@ -461,7 +498,7 @@ void VApplication::BeginLogging() qInstallMessageHandler(noisyFailureMsgHandler); logLock = new QLockFile(LogPath()+".lock"); logLock->setStaleLockTime(0); - if (logLock->tryLock()) + if (TryLock(logLock)) { qCDebug(vApp) << "Log file"<tryLock()) + if (TryLock(lock)) { qCDebug(vApp) << "Locked file"<setStaleLockTime(0); - if (logLock->tryLock()) + if (TryLock(logLock)) { *out <<"--------------------------" << endl; QFile logFile(info.absoluteFilePath()); diff --git a/src/app/core/vapplication.h b/src/app/core/vapplication.h index 7122d424d..93a864d50 100644 --- a/src/app/core/vapplication.h +++ b/src/app/core/vapplication.h @@ -70,6 +70,8 @@ public: double fromPixel(double pix, const Unit &unit) const; double fromPixel(double pix) const; + static bool TryLock(QLockFile *lock); + static const qreal PrintDPI; QString translationsPath() const; QString pathToTables() const; diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 01097d8de..f76099bd1 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -2365,7 +2365,7 @@ void MainWindow::LoadPattern(const QString &fileName) qCDebug(vMainWindow)<<"Loking file"; lock = new QLockFile(fileName+".lock"); lock->setStaleLockTime(0); - if (lock->tryLock()) + if (VApplication::TryLock(lock)) { qCDebug(vMainWindow) << "Pattern file"<setStaleLockTime(0); - if (lock->tryLock()) + if (VApplication::TryLock(lock)) { restoreFiles.append(files.at(i)); }