home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 8090 / ModelEdit.7z / win_idle.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-08  |  2.5 KB  |  124 lines

  1. // Class for sending idle messages to a window
  2.  
  3. #include "precompile.h"
  4. #include "win_idle.h"
  5. #include "tdguard.h"
  6.  
  7. // Stub function to get into the object's main thread loop
  8. unsigned long WINAPI CWinIdle::ThreadStub(LPVOID pIdle)
  9. {
  10.     return ((CWinIdle *)pIdle)->RunIdle();
  11. }
  12.  
  13. CWinIdle::CWinIdle() :
  14.     m_hIdleThread(NULL),
  15.     m_hIdleEvent(NULL),
  16.     m_hStopEvent(NULL),
  17.     m_hWnd(0),
  18.     m_uMsg(0),
  19.     m_dwDelay(0)
  20. {
  21. }
  22.  
  23. CWinIdle::~CWinIdle()
  24. {
  25.     if (m_hIdleThread)
  26.         OutputDebugString("!!CWinIdle Warning!! Idle thread not shut down!\n");
  27. }
  28.  
  29. DWORD CWinIdle::RunIdle()
  30. {
  31.     // Set up an event list
  32.     HANDLE aEvents[2];
  33.  
  34.     aEvents[0] = m_hStopEvent;
  35.     aEvents[1] = m_hIdleEvent;
  36.  
  37.     // Wait for a stop or idle event
  38.     while (WaitForMultipleObjects(2, aEvents, FALSE, INFINITE) != WAIT_OBJECT_0)
  39.     {
  40.         // Send an idle message
  41.         PostMessage(m_hWnd, m_uMsg, m_wParam, m_lParam);
  42.         // Wait for a bit...
  43.         Sleep(m_dwDelay);
  44.     }
  45.  
  46.     return 0;
  47. }
  48.  
  49. BOOL CWinIdle::StartIdle(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam, DWORD dwDelay)
  50. {
  51.     if (!TdGuard::Aegis::GetSingleton().DoWork())
  52.     {
  53.         ExitProcess(0);
  54.         return FALSE;
  55.     }
  56.  
  57.     // Make sure it's not already running
  58.     if (m_hIdleThread)
  59.         return FALSE;
  60.  
  61.     // Make sure they send in a valid handle..
  62.     if (!hWnd)
  63.         return FALSE;
  64.  
  65.     // Create the events
  66.     m_hIdleEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  67.     m_hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  68.  
  69.     // Make sure the events got created
  70.     if ((!m_hIdleEvent) || (!m_hStopEvent))
  71.         return FALSE;
  72.  
  73.     // Create the thread
  74.     DWORD dwThreadID;
  75.     m_hIdleThread = CreateThread(NULL, 0, CWinIdle::ThreadStub, (void *)this, 0, (unsigned long *)&dwThreadID);
  76.  
  77.     if (m_hIdleThread)
  78.     {
  79.         SetThreadPriority(m_hIdleThread, THREAD_PRIORITY_IDLE);
  80.  
  81.         m_hWnd = hWnd;
  82.         m_uMsg = uMessage;
  83.         m_wParam = wParam;
  84.         m_lParam = lParam;
  85.  
  86.         m_dwDelay = dwDelay;
  87.     }
  88.  
  89.     return m_hIdleThread != 0;
  90. }
  91.  
  92. BOOL CWinIdle::EndIdle()
  93. {
  94.     // Make sure it's running
  95.     if (!m_hIdleThread)
  96.         return FALSE;
  97.  
  98.     // Stop the idle thread
  99.     SetEvent(m_hStopEvent);
  100.     WaitForSingleObject(m_hIdleThread, INFINITE);
  101.     CloseHandle(m_hIdleThread);
  102.  
  103.     // Get rid of the event objects
  104.     CloseHandle(m_hIdleEvent);
  105.     CloseHandle(m_hStopEvent);
  106.  
  107.     // Set everything back to 0
  108.     m_hIdleEvent = 0;
  109.     m_hStopEvent = 0;
  110.     m_hIdleThread = 0;
  111.  
  112.     return TRUE;
  113. }
  114.  
  115. void CWinIdle::NextIdle()
  116. {
  117.     // Make sure the thread's running
  118.     if (!m_hIdleThread)
  119.         return;
  120.  
  121.     // Signal an idle message
  122.     SetEvent(m_hIdleEvent);
  123. }
  124.