mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-24 00:14:06 +00:00
prevention of 2nd copy start: algorithm changed (should work better)
This commit is contained in:
parent
ded22cc3fd
commit
b14a6271d9
22
src/main.cc
22
src/main.cc
|
@ -42,10 +42,13 @@ int main( int argc, char ** argv )
|
|||
// get pid
|
||||
quint64 current_pid = QCoreApplication::applicationPid();
|
||||
|
||||
QString app_fname = QFileInfo(QCoreApplication::applicationFilePath()).baseName();
|
||||
quint64 pid = ProcessWrapper::findProcess(
|
||||
app_fname.toAscii().data(),
|
||||
current_pid);
|
||||
// QString app_fname = QFileInfo(QCoreApplication::applicationFilePath()).baseName();
|
||||
// quint64 pid = ProcessWrapper::findProcess(
|
||||
// app_fname.toAscii().data(),
|
||||
// current_pid);
|
||||
//
|
||||
// qDebug() << "pid " << pid;
|
||||
// qDebug() << "current_pid " << current_pid;
|
||||
|
||||
// check pid file
|
||||
QFile pid_file( Config::getPidFileName() );
|
||||
|
@ -57,11 +60,18 @@ int main( int argc, char ** argv )
|
|||
quint64 tmp; ds >> tmp;
|
||||
pid_file.close();
|
||||
|
||||
if ( tmp == pid ) // it is active - exiting
|
||||
bool isExist = ProcessWrapper::processExists(tmp);
|
||||
if ( isExist )
|
||||
{
|
||||
// to do: switch to pid ?
|
||||
puts( "Another GoldenDict copy started already." );
|
||||
return 1;
|
||||
}
|
||||
|
||||
// if ( tmp == pid ) // it is active - exiting
|
||||
// {
|
||||
// // to do: switch to pid ?
|
||||
// return 1;
|
||||
// }
|
||||
}
|
||||
|
||||
pid_file.open( QIODevice::WriteOnly );
|
||||
|
|
|
@ -13,6 +13,25 @@ unsigned int ProcessWrapper::currentProcessId()
|
|||
return GetCurrentProcessId();
|
||||
}
|
||||
|
||||
bool ProcessWrapper::processExists(unsigned int pid)
|
||||
{
|
||||
DWORD aProcesses[1024], cbNeeded, cProcesses;
|
||||
unsigned int i;
|
||||
|
||||
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
|
||||
return false;
|
||||
|
||||
cProcesses = cbNeeded / sizeof(DWORD);
|
||||
for ( i = 0; i < cProcesses; i++ )
|
||||
{
|
||||
unsigned int processID = aProcesses[i];
|
||||
if ( processID == pid )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int ProcessWrapper::findProcess(const char *name, unsigned int pid_skip)
|
||||
{
|
||||
DWORD aProcesses[1024], cbNeeded, cProcesses;
|
||||
|
@ -31,7 +50,7 @@ unsigned int ProcessWrapper::findProcess(const char *name, unsigned int pid_skip
|
|||
for ( i = 0; i < cProcesses; i++ )
|
||||
{
|
||||
unsigned int processID = aProcesses[i];
|
||||
if( processID != 0 && processID != pid_skip )
|
||||
if( processID != 0 && processID != pid_skip )
|
||||
{
|
||||
char szProcessName[MAX_PATH] = "<unknown>";
|
||||
|
||||
|
@ -80,6 +99,11 @@ unsigned int ProcessWrapper::currentProcessId()
|
|||
return getpid();
|
||||
}
|
||||
|
||||
bool ProcessWrapper::processExists(unsigned int pid)
|
||||
{
|
||||
return QFile::exists(QString("/proc/%1").arg(pid));
|
||||
}
|
||||
|
||||
unsigned int ProcessWrapper::findProcess(const char *name, unsigned int pid_skip)
|
||||
{
|
||||
QString pname("(" + QString(name) + ")");
|
||||
|
|
|
@ -4,10 +4,11 @@
|
|||
class ProcessWrapper
|
||||
{
|
||||
public:
|
||||
ProcessWrapper();
|
||||
ProcessWrapper();
|
||||
|
||||
static unsigned int findProcess(const char *name, unsigned int pid_skip = 0);
|
||||
static unsigned int findProcess(const char *name, unsigned int pid_skip = 0);
|
||||
static unsigned int currentProcessId();
|
||||
static bool processExists(unsigned int pid);
|
||||
};
|
||||
|
||||
#endif // PROCESSWRAPPER_H
|
||||
|
|
Loading…
Reference in a new issue