31#include <wx/filename.h>
38static const char*
const kName =
"_OpenCPN_SILock";
40static void KillProcess(
int pid) {
42 if (GetCurrentProcessId() != pid) {
43 const auto proc = OpenProcess(PROCESS_TERMINATE,
false, pid);
44 TerminateProcess(proc, 1);
48 if (pid != getpid()) kill(
static_cast<pid_t
>(pid), SIGKILL);
56WxInstanceCheck::WxInstanceCheck()
57 : m_checker(new wxSingleInstanceChecker), is_inited(false) {}
59void WxInstanceCheck::Init() {
62 if (!m_checker->Create(kName, dir)) {
63 WARNING_LOG <<
"Cannot create instance locker (!)";
69 if (!is_inited) Init();
70 return !m_checker->IsAnotherRunning();
74 if (!is_inited) Init();
76 if (!wxFileExists(lockfile.GetFullPath()))
return;
80 std::ifstream f(lockfile.GetFullPath().ToStdString());
85 pid = std::stoi(ss.str());
89 wxRemoveFile(lockfile.GetFullPath());
90 if (pid != -1) KillProcess(pid);
94 if (!is_inited) Init();
void OnExit() override
Do whatever needed before wxWidget's checks triggers.
bool IsMainInstance() override
Return true if this process is the primary opencpn instance.
void CleanUp() override
Remove all persistent instance state, including possible lock file and defunct opencpn processes.
Enhanced logging interface on top of wx/log.h.
Single instance check based on wxWidgets functions.