-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_instance.cpp
More file actions
53 lines (46 loc) · 1.28 KB
/
Copy pathsingle_instance.cpp
File metadata and controls
53 lines (46 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "single_instance.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif
SingleInstance::SingleInstance(QObject *parent) : QObject(parent) {
m_pollTimer.setInterval(140);
connect(&m_pollTimer, &QTimer::timeout, this, [this]() {
#ifdef Q_OS_WIN
if (m_eventHandle
&& WaitForSingleObject(static_cast<HANDLE>(m_eventHandle), 0) == WAIT_OBJECT_0) {
emit showRequested();
}
#endif
});
}
SingleInstance::~SingleInstance() {
#ifdef Q_OS_WIN
if (m_eventHandle)
CloseHandle(static_cast<HANDLE>(m_eventHandle));
#endif
}
bool SingleInstance::startOrNotifyExisting() {
#ifdef Q_OS_WIN
const wchar_t *eventName = L"Local\\PickMoji-show";
HANDLE eventHandle = CreateEventW(nullptr, FALSE, FALSE, eventName);
if (!eventHandle)
return true; // Do not prevent startup if the OS event cannot be created.
if (GetLastError() == ERROR_ALREADY_EXISTS) {
SetEvent(eventHandle);
CloseHandle(eventHandle);
return false;
}
m_eventHandle = eventHandle;
m_pollTimer.start();
#endif
return true;
}
void SingleInstance::release() {
#ifdef Q_OS_WIN
m_pollTimer.stop();
if (m_eventHandle) {
CloseHandle(static_cast<HANDLE>(m_eventHandle));
m_eventHandle = nullptr;
}
#endif
}