GrabBag/AppUtils/AppCommon/Inc/SingleInstanceManager.h

61 lines
1.6 KiB
C
Raw Normal View History

#ifndef SINGLEINSTANCEMANAGER_H
#define SINGLEINSTANCEMANAGER_H
#include <QString>
#include <QSharedMemory>
#include <QSystemSemaphore>
#include <memory>
/**
* @brief
*
*
* 使
*
*
* @code
* SingleInstanceManager manager("MyApp");
* if (manager.isAnotherInstanceRunning()) {
* QMessageBox::information(nullptr, "应用程序已运行", "应用程序已经在运行中!");
* return 0;
* }
* @endcode
*/
class SingleInstanceManager
{
public:
/**
* @brief
* @param appKey 使
*/
explicit SingleInstanceManager(const QString& appKey);
/**
* @brief
*/
~SingleInstanceManager();
/**
* @brief
* @return true: , false:
*/
bool isAnotherInstanceRunning() const;
/**
* @brief
*/
void release();
private:
QString m_appKey;
std::unique_ptr<QSystemSemaphore> m_semaphore;
std::unique_ptr<QSharedMemory> m_sharedMemory;
bool m_isRunning;
// 禁止拷贝和赋值
SingleInstanceManager(const SingleInstanceManager&) = delete;
SingleInstanceManager& operator=(const SingleInstanceManager&) = delete;
};
#endif // SINGLEINSTANCEMANAGER_H