home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / BaseClasses / msgthrd.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  3.4 KB  |  121 lines

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