*/
CDiffThread::CDiffThread()
: m_pDiffContext(NULL)
-, m_msgUpdateUI(0)
-, m_hWnd(0)
, m_bAborting(false)
, m_pDiffParm(new DiffFuncStruct)
{
assert(m_pDiffParm->nThreadState != THREAD_COMPARING);
m_pDiffParm->context = m_pDiffContext;
- m_pDiffParm->msgUIUpdate = m_msgUpdateUI;
- m_pDiffParm->hWindow = m_hWnd;
m_pDiffParm->m_pAbortgate = m_pAbortgate.get();
m_pDiffParm->bOnlyRequested = m_bOnlyRequested;
m_bAborting = false;
}
/**
- * @brief Set window receiving messages thread sends.
- * @param [in] hWnd Hand to window to receive messages.
- */
-void CDiffThread::SetHwnd(HWND hWnd)
-{
- m_hWnd = hWnd;
-}
-
-/**
- * @brief Set message-id for update message.
- * @param [in] updateMsg Message-id for update message.
- */
-void CDiffThread::SetMessageIDs(unsigned updateMsg)
-{
- m_msgUpdateUI = updateMsg;
-}
-
-/**
* @brief Selects to compare all or only selected items.
* @param [in] bSelected If true only selected items are compared.
*/
myStruct->pSemaphore->set();
// Send message to UI to update
- PostMessage(myStruct->hWindow, myStruct->msgUIUpdate, 2, myStruct->bOnlyRequested);
+ int event = CDiffThread::EVENT_COLLECT_COMPLETED;
+ myStruct->m_listeners.notify(myStruct, event);
};
/**
// Send message to UI to update
myStruct->nThreadState = CDiffThread::THREAD_COMPLETED;
- // msgID=MSG_UI_UPDATE=1025 (2005-11-29, Perry)
- PostMessage(myStruct->hWindow, myStruct->msgUIUpdate, 0, myStruct->bOnlyRequested);
+ int event = CDiffThread::EVENT_COMPARE_COMPLETED;
+ myStruct->m_listeners.notify(myStruct, event);
}
#include <boost/scoped_ptr.hpp>
#include <Poco/Thread.h>
+#include <Poco/BasicEvent.h>
+#include <Poco/Delegate.h>
#include "DiffContext.h"
-#include <windows.h>
namespace Poco
{
class Semaphore;
}
-struct DiffFuncStruct;
class DiffThreadAbortable;
-class CWinThread;
+
+/**
+ * @brief Structure used in sending data to the threads.
+ * As thread functions have only one parameter we must pack all
+ * the data we need inside structure.
+ */
+struct DiffFuncStruct
+{
+ CDiffContext * context; /**< Compare context. */
+ Poco::BasicEvent<int> m_listeners; /**< Event listeners */
+ int nThreadState; /**< Thread state. */
+ DiffThreadAbortable * m_pAbortgate; /**< Interface for aborting compare. */
+ bool bOnlyRequested; /**< Compare only requested items? */
+ Poco::Semaphore *pSemaphore; /**< Semaphore for synchronizing threads. */
+
+ DiffFuncStruct()
+ : context(NULL)
+ , nThreadState(0/*CDiffThread::THREAD_NOTSTARTED*/)
+ , m_pAbortgate(NULL)
+ , bOnlyRequested(false)
+ , pSemaphore(NULL)
+ {}
+};
/**
* @brief Class for threaded folder compare.
THREAD_COMPLETED, /**< Thread has completed its task. */
};
+ enum ThreadEvent
+ {
+ EVENT_COLLECT_COMPLETED = 2,
+ EVENT_COMPARE_PROGRESSED = 1,
+ EVENT_COMPARE_COMPLETED = 0,
+ };
+
// creation and use, called on main thread
CDiffThread();
~CDiffThread();
void SetContext(CDiffContext * pCtx);
unsigned CompareDirectories();
- void SetHwnd(HWND hWnd);
- void SetMessageIDs(unsigned updateMsg);
+ template<class T>
+ void AddListener(T *pObj, void (T::*pMethod)(int& state)) {
+ m_pDiffParm->m_listeners += Poco::delegate(pObj, pMethod);
+ }
+ template<class T>
+ void RemoveListener(T *pObj, void (T::*pMethod)(int& state)) {
+ m_pDiffParm->m_listeners -= Poco::delegate(pObj, pMethod);
+ }
void SetCompareSelected(bool bSelected = false);
// runtime interface for main thread, called on main thread
Poco::Thread m_threads[2]; /**< Compare threads. */
boost::scoped_ptr<DiffFuncStruct> m_pDiffParm; /**< Structure for sending data to threads. */
boost::scoped_ptr<DiffThreadAbortable> m_pAbortgate;
- unsigned m_msgUpdateUI; /**< UI-update message number */
- HWND m_hWnd; /**< Handle to folder compare GUI window */
bool m_bAborting; /**< Is compare aborting? */
bool m_bOnlyRequested; /**< Are we comparing only requested items (Update?) */
};
-/**
- * @brief Structure used in sending data to the threads.
- * As thread functions have only one parameter we must pack all
- * the data we need inside structure.
- */
-struct DiffFuncStruct
-{
- CDiffContext * context; /**< Compare context. */
- unsigned msgUIUpdate; /**< Windows message for updating GUI. */
- HWND hWindow; /**< Window getting status updates. */
- CDiffThread::ThreadState nThreadState; /**< Thread state. */
- DiffThreadAbortable * m_pAbortgate; /**< Interface for aborting compare. */
- bool bOnlyRequested; /**< Compare only requested items? */
- Poco::Semaphore *pSemaphore; /**< Semaphore for synchronizing threads. */
-
- DiffFuncStruct()
- : context(NULL)
- , msgUIUpdate(0)
- , hWindow(0)
- , nThreadState(CDiffThread::THREAD_NOTSTARTED)
- , m_pAbortgate(NULL)
- , bOnlyRequested(false)
- , pSemaphore(NULL)
- {}
-};
-
#endif /* _DIFFTHREAD_H */
m_pCtxt->m_pFilterList->AddRegExp(*it);\r
}
+void CDirDoc::DiffThreadCallback(int& state)
+{
+ PostMessage(m_pDirView->GetSafeHwnd(), MSG_UI_UPDATE, state, m_bMarkedRescan);
+}
+
/**
* @brief Perform directory comparison again from scratch
*/
// Folder names to compare are in the compare context
m_diffThread.SetContext(m_pCtxt.get());
- m_diffThread.SetHwnd(m_pDirView->GetSafeHwnd());
- m_diffThread.SetMessageIDs(MSG_UI_UPDATE);
+ m_diffThread.AddListener(this, &CDirDoc::DiffThreadCallback);
m_diffThread.SetCompareSelected(!!m_bMarkedRescan);
m_diffThread.CompareDirectories();
m_bMarkedRescan = FALSE;
// Implementation
public:
void InitCompare(const PathContext & paths, bool bRecursive, CTempPathContext *);
+ void DiffThreadCallback(int& state);
void Rescan();
bool GetRecursive() const { return m_bRecursive; }
bool GetReadOnly(int nIndex) const;
}
if (GetTickCount() - dwElapse > 2000)
{
- PostMessage(myStruct->hWindow, myStruct->msgUIUpdate, 1, myStruct->bOnlyRequested);
+ int event = CDiffThread::EVENT_COMPARE_PROGRESSED;
+ myStruct->m_listeners.notify(myStruct, event);
dwElapse = GetTickCount();
}
myStruct->pSemaphore->wait();
CDirDoc * pDoc = GetDocument();
ASSERT(pDoc);
- if (wParam == 0)
+ if (wParam == CDiffThread::EVENT_COMPARE_COMPLETED)
{
// Close and destroy the dialog after compare
m_pCmpProgressDlg->CloseDialog();
MessageBeep(IDOK);
GetMainFrame()->StartFlashing();
}
- else if (wParam == 1)
+ else if (wParam == CDiffThread::EVENT_COMPARE_PROGRESSED)
{
InvalidateRect(NULL, FALSE);
}
- else if (wParam == 2)
+ else if (wParam == CDiffThread::EVENT_COLLECT_COMPLETED)
{
if (m_pSavedTreeState)
{
#include "DiffContext.h"\r
#include "CompareStats.h"\r
+#include "DiffThread.h"\r
#include "DiffWrapper.h"\r
#include "FileFilterHelper.h"\r
-#include "DiffThread.h"\r
#include <Poco/Thread.h>\r
\r
int main()\r
// Folder names to compare are in the compare context
CDiffThread diffThread;
diffThread.SetContext(&ctx);
- diffThread.SetHwnd(NULL);
- diffThread.SetMessageIDs(1);
diffThread.SetCompareSelected(false);
diffThread.CompareDirectories();