Skip to content

Commit 6d430e0

Browse files
committed
Update SingleApplication
1 parent 9022c1d commit 6d430e0

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎src/main.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int main(int argc, char *argv[])
6666

6767
// GUI application /////////////////////////////////////////////////////////
6868

69-
SingleApplication app(argc, argv, true);
69+
SingleApplication app(argc, argv, false);
7070

7171
// Application name
7272
app.setApplicationName("toolBLEx");

‎thirdparty/SingleApplication/SingleApplication.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,36 @@
2525
#include <QtCore/QElapsedTimer>
2626
#include <QtCore/QSharedMemory>
2727

28+
#ifdef Q_OS_UNIX
29+
#include <signal.h>
30+
#include <errno.h>
31+
#endif
32+
#ifdef Q_OS_WIN
33+
#include <windows.h>
34+
#endif
35+
2836
#include "SingleApplication.h"
2937
#include "SingleApplication_private.h"
3038

39+
static bool isProcessRunning( qint64 pid )
40+
{
41+
if ( pid <= 0 )
42+
return false;
43+
44+
#ifdef Q_OS_UNIX
45+
return kill( static_cast<pid_t>( pid ), 0 ) == 0 || errno != ESRCH;
46+
#endif
47+
#ifdef Q_OS_WIN
48+
HANDLE hProcess = OpenProcess( PROCESS_QUERY_LIMITED_INFORMATION, FALSE, static_cast<DWORD>( pid ) );
49+
if ( hProcess == NULL )
50+
return false;
51+
DWORD exitCode;
52+
bool running = GetExitCodeProcess( hProcess, &exitCode ) && exitCode == STILL_ACTIVE;
53+
CloseHandle( hProcess );
54+
return running;
55+
#endif
56+
}
57+
3158
/**
3259
* @brief Constructor. Checks and fires up LocalServer or closes the program
3360
* if another instance already exists
@@ -134,6 +161,13 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
134161
}
135162
}
136163

164+
// If the recorded primary PID is no longer running (e.g. force-killed),
165+
// take over as the primary instance
166+
if( inst->primary && !isProcessRunning( inst->primaryPid ) ){
167+
qWarning() << "SingleApplication: Primary instance (PID" << inst->primaryPid << ") is no longer running. Taking over.";
168+
d->initializeMemoryBlock();
169+
}
170+
137171
if( inst->primary == false ){
138172
d->startPrimary();
139173
if( ! d->memory->unlock() ){

0 commit comments

Comments
 (0)