Andreas Traczyk | 45f56c9 | 2019-01-06 10:04:44 -0500 | [diff] [blame] | 1 | // From: https://stackoverflow.com/a/28172162 |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <QObject> |
| 6 | #include <QSharedMemory> |
| 7 | #include <QSystemSemaphore> |
| 8 | |
| 9 | class RunGuard |
| 10 | { |
| 11 | |
| 12 | public: |
| 13 | RunGuard(const QString& key); |
| 14 | ~RunGuard(); |
| 15 | |
| 16 | bool isAnotherRunning(); |
| 17 | bool tryToRun(); |
| 18 | void release(); |
| 19 | |
| 20 | private: |
| 21 | const QString key; |
| 22 | const QString memLockKey; |
| 23 | const QString sharedmemKey; |
| 24 | |
| 25 | QSharedMemory sharedMem; |
| 26 | QSystemSemaphore memLock; |
| 27 | |
| 28 | Q_DISABLE_COPY(RunGuard) |
| 29 | }; |