home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / msgthrd.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-19  |  3.6 KB  |  126 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. // support for a worker thread class to which you can asynchronously post
  13. // messages
  14.  
  15.  
  16. // Message class - really just a structure.
  17. //
  18. class CMsg {
  19. public:
  20.     UINT uMsg;
  21.     DWORD dwFlags;
  22.     LPVOID lpParam;
  23.     CAMEvent *pEvent;
  24.  
  25.     CMsg(UINT u, DWORD dw, LPVOID lp, CAMEvent *pEvnt)
  26.         : uMsg(u), dwFlags(dw), lpParam(lp), pEvent(pEvnt) {}
  27.  
  28.     CMsg()
  29.         : uMsg(0), dwFlags(0L), lpParam(NULL), pEvent(NULL) {}
  30. };
  31.  
  32. // This is the actual thread class.  It exports all the usual thread control
  33. // functions.  The created thread is different from a normal WIN32 thread in
  34. // that it is prompted to perform particaular tasks by responding to messages
  35. // posted to its message queue.
  36. //
  37. class CMsgThread {
  38. private:
  39.     static DWORD WINAPI DefaultThreadProc(LPVOID lpParam);
  40.     DWORD               m_ThreadId;
  41.     HANDLE              m_hThread;
  42.  
  43. protected:
  44.  
  45.     // if you want to override GetThreadMsg to block on other things
  46.     // as well as this queue, you need access to this
  47.     CGenericList<CMsg>        m_ThreadQueue;
  48.     CCritSec                  m_Lock;
  49.     HANDLE                    m_hSem;
  50.     LONG                      m_lWaiting;
  51.  
  52. public:
  53.     CMsgThread()
  54.         : m_ThreadId(0),
  55.         m_hThread(NULL),
  56.         m_lWaiting(0),
  57.         m_hSem(NULL),
  58.         // make a list with a cache of 5 items
  59.         m_ThreadQueue(NAME("MsgThread list"), 5)
  60.         {
  61.         }
  62.  
  63.     ~CMsgThread();
  64.     // override this if you want to block on other things as well
  65.     // as the message loop
  66.     void virtual GetThreadMsg(CMsg *msg);
  67.  
  68.     // override this if you want to do something on thread startup
  69.     virtual void OnThreadInit() {
  70.     };
  71.  
  72.     BOOL CreateThread();
  73.  
  74.     BOOL WaitForThreadExit(LPDWORD lpdwExitCode) {
  75.         if (m_hThread != NULL) {
  76.             WaitForSingleObject(m_hThread, INFINITE);
  77.             return GetExitCodeThread(m_hThread, lpdwExitCode);
  78.         }
  79.         return FALSE;
  80.     }
  81.  
  82.     DWORD ResumeThread() {
  83.         return ::ResumeThread(m_hThread);
  84.     }
  85.  
  86.     DWORD SuspendThread() {
  87.         return ::SuspendThread(m_hThread);
  88.     }
  89.  
  90.     int GetThreadPriority() {
  91.         return ::GetThreadPriority(m_hThread);
  92.     }
  93.  
  94.     BOOL SetThreadPriority(int nPriority) {
  95.         return ::SetThreadPriority(m_hThread, nPriority);
  96.     }
  97.  
  98.     HANDLE GetThreadHandle() {
  99.         return m_hThread;
  100.     }
  101.  
  102.     DWORD GetThreadId() {
  103.         return m_ThreadId;
  104.     }
  105.  
  106.  
  107.     void PutThreadMsg(UINT uMsg, DWORD dwMsgFlags,
  108.                       LPVOID lpMsgParam, CAMEvent *pEvent = NULL) {
  109.         CAutoLock lck(&m_Lock);
  110.         CMsg* pMsg = new CMsg(uMsg, dwMsgFlags, lpMsgParam, pEvent);
  111.         m_ThreadQueue.AddTail(pMsg);
  112.         if (m_lWaiting != 0) {
  113.             ReleaseSemaphore(m_hSem, m_lWaiting, 0);
  114.             m_lWaiting = 0;
  115.         }
  116.     }
  117.  
  118.     // This is the function prototype of the function that the client
  119.     // supplies.  It is always called on the created thread, never on
  120.     // the creator thread.
  121.     //
  122.     virtual LRESULT ThreadMessageProc(
  123.         UINT uMsg, DWORD dwFlags, LPVOID lpParam, CAMEvent *pEvent) = 0;
  124. };
  125.  
  126.