From 72a9145a5df89c4c26895df8ddf4832c7bd1e226 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Tue, 21 Feb 2012 01:59:44 +0100 Subject: [PATCH] Improved IPC behavior when the running instance does not respond for whatever reason. --- src/thread_ipc.cpp | 27 ++++++++++----------------- src/thread_ipc.h | 1 - src/version.h | 2 +- src/win_main.cpp | 11 ++++++++--- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/thread_ipc.cpp b/src/thread_ipc.cpp index 7a2b8ca..45fe4e3 100644 --- a/src/thread_ipc.cpp +++ b/src/thread_ipc.cpp @@ -57,6 +57,7 @@ bool IPCThread::initialize(bool *firstInstance) if(m_sharedMem->create(sizeof(DWORD), QSharedMemory::ReadWrite)) { m_firstInstance = m_ipcReady = true; + memset(m_sharedMem->data(), 0, sizeof(DWORD)); } else { @@ -76,17 +77,6 @@ bool IPCThread::initialize(bool *firstInstance) return m_ipcReady; } -void IPCThread::notifyOtherInstance(void) -{ - if(!m_firstInstance) - { - m_semaphore_w->acquire(); - DWORD *pidPtr = reinterpret_cast(m_sharedMem->data()); - *pidPtr = GetCurrentProcessId(); - m_semaphore_r->release(); - } -} - void IPCThread::start(Priority priority) { m_abort = false; @@ -96,12 +86,6 @@ void IPCThread::start(Priority priority) throw "IPC not initialized yet !!!"; return; } - - if(!m_firstInstance) - { - qWarning("This is NOT the first instance!"); - return; - } QThread::start(priority); } @@ -117,6 +101,15 @@ void IPCThread::setAbort(void) void IPCThread::run(void) { + if(!m_firstInstance) + { + m_semaphore_w->acquire(); + DWORD *pidPtr = reinterpret_cast(m_sharedMem->data()); + *pidPtr = GetCurrentProcessId(); + m_semaphore_r->release(); + return; + } + forever { if(!m_semaphore_r->acquire()) diff --git a/src/thread_ipc.h b/src/thread_ipc.h index 25f4d10..e054e5f 100644 --- a/src/thread_ipc.h +++ b/src/thread_ipc.h @@ -37,7 +37,6 @@ public: void setAbort(void); bool initialize(bool *firstInstance = NULL); - void notifyOtherInstance(void); signals: void instanceCreated(DWORD pid); diff --git a/src/version.h b/src/version.h index bdc738b..f44eceb 100644 --- a/src/version.h +++ b/src/version.h @@ -22,7 +22,7 @@ #define VER_X264_MAJOR 2 #define VER_X264_MINOR 0 #define VER_X264_PATCH 2 -#define VER_X264_BUILD 238 +#define VER_X264_BUILD 243 #define VER_X264_MINIMUM_REV 2146 #define VER_X264_CURRENT_API 120 diff --git a/src/win_main.cpp b/src/win_main.cpp index fcb3b0f..daf37a7 100644 --- a/src/win_main.cpp +++ b/src/win_main.cpp @@ -638,12 +638,17 @@ void MainWindow::init(void) bool firstInstance = false; if(m_ipcThread->initialize(&firstInstance)) { + m_ipcThread->start(); if(!firstInstance) { - m_ipcThread->notifyOtherInstance(); + if(!m_ipcThread->wait(5000)) + { + QMessageBox::warning(this, tr("Not Responding"), tr("Another instance of this application is already running, but did not respond in time.
If the problem persists, please kill the running instance from the task manager!
"), tr("Quit")); + m_ipcThread->terminate(); + m_ipcThread->wait(); + } close(); qApp->exit(-1); return; } - m_ipcThread->start(); } //Check all binaries @@ -831,7 +836,7 @@ void MainWindow::handleDroppedFiles(void) void MainWindow::instanceCreated(DWORD pid) { - qDebug("Notification from other instance received!"); + qDebug("Notification from other instance (PID=0x%X) received!", pid); FLASHWINFO flashWinInfo; memset(&flashWinInfo, 0, sizeof(FLASHWINFO)); -- 2.11.0