AJA NTV2 SDK  18.0.0.2717
NTV2 SDK 18.0.0.2717
msgthrd.h
Go to the documentation of this file.
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;
19 
20  CMsg(UINT u, DWORD dw, __inout_opt LPVOID lp, __in_opt 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(__inout 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
45  LONG m_lWaiting;
46 
47 public:
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(__out CMsg *msg);
62 
63  // override this if you want to do something on thread startup
64  virtual void OnThreadInit() {
65  };
66 
67  BOOL CreateThread();
68 
69  BOOL WaitForThreadExit(__out LPDWORD lpdwExitCode) {
70  if (m_hThread != NULL) {
71  WaitForSingleObject(m_hThread, INFINITE);
72  return GetExitCodeThread(m_hThread, lpdwExitCode);
73  }
74  return FALSE;
75  }
76 
77  DWORD ResumeThread() {
78  return ::ResumeThread(m_hThread);
79  }
80 
81  DWORD SuspendThread() {
82  return ::SuspendThread(m_hThread);
83  }
84 
86  return ::GetThreadPriority(m_hThread);
87  }
88 
89  BOOL SetThreadPriority(int nPriority) {
90  return ::SetThreadPriority(m_hThread, nPriority);
91  }
92 
94  return m_hThread;
95  }
96 
97  DWORD GetThreadId() {
98  return m_ThreadId;
99  }
100 
101 
102  void PutThreadMsg(UINT uMsg, DWORD dwMsgFlags,
103  __in_opt LPVOID lpMsgParam, __in_opt CAMEvent *pEvent = NULL) {
104  CAutoLock lck(&m_Lock);
105  CMsg* pMsg = new CMsg(uMsg, dwMsgFlags, lpMsgParam, pEvent);
106  m_ThreadQueue.AddTail(pMsg);
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, __inout_opt LPVOID lpParam, __in_opt CAMEvent *pEvent) = 0;
119 };
120 
CMsgThread::WaitForThreadExit
BOOL WaitForThreadExit(__out LPDWORD lpdwExitCode)
Definition: msgthrd.h:69
CMsg::CMsg
CMsg(UINT u, DWORD dw, __inout_opt LPVOID lp, __in_opt CAMEvent *pEvnt)
Definition: msgthrd.h:20
HANDLE
short HANDLE
Definition: ajatypes.h:338
CGenericList< CMsg >
CMsgThread::m_ThreadQueue
CGenericList< CMsg > m_ThreadQueue
Definition: msgthrd.h:42
NULL
#define NULL
Definition: ntv2caption608types.h:19
CMsgThread::PutThreadMsg
void PutThreadMsg(UINT uMsg, DWORD dwMsgFlags, __in_opt LPVOID lpMsgParam, __in_opt CAMEvent *pEvent=NULL)
Definition: msgthrd.h:102
NAME
#define NAME(_x_)
Definition: wxdebug.h:179
CAutoLock
Definition: wxutil.h:83
CMsg::pEvent
CAMEvent * pEvent
Definition: msgthrd.h:18
CMsgThread::m_lWaiting
LONG m_lWaiting
Definition: msgthrd.h:45
CMsgThread::SuspendThread
DWORD SuspendThread()
Definition: msgthrd.h:81
CMsg::lpParam
LPVOID lpParam
Definition: msgthrd.h:17
CMsgThread::GetThreadPriority
int GetThreadPriority()
Definition: msgthrd.h:85
CMsgThread::CMsgThread
CMsgThread()
Definition: msgthrd.h:48
CCritSec
Definition: wxutil.h:18
CAMEvent
Definition: wxutil.h:108
CMsgThread::m_hSem
HANDLE m_hSem
Definition: msgthrd.h:44
CMsg::CMsg
CMsg()
Definition: msgthrd.h:23
CMsg::uMsg
UINT uMsg
Definition: msgthrd.h:15
CMsgThread::m_Lock
CCritSec m_Lock
Definition: msgthrd.h:43
CMsg
Definition: msgthrd.h:13
CMsgThread::OnThreadInit
virtual void OnThreadInit()
Definition: msgthrd.h:64
CGenericList::AddTail
__out_opt POSITION AddTail(__in OBJECT *pObj)
Definition: wxlist.h:533
CMsg::dwFlags
DWORD dwFlags
Definition: msgthrd.h:16
CMsgThread::GetThreadHandle
HANDLE GetThreadHandle()
Definition: msgthrd.h:93
CMsgThread::ResumeThread
DWORD ResumeThread()
Definition: msgthrd.h:77
CMsgThread
Definition: msgthrd.h:32
CMsgThread::GetThreadId
DWORD GetThreadId()
Definition: msgthrd.h:97
CMsgThread::SetThreadPriority
BOOL SetThreadPriority(int nPriority)
Definition: msgthrd.h:89