Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATMsgThread.cpp Source File

CATMsgThread.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file    CATMsgThread.cpp
00003 /// \brief   Message Thread Base Class
00004 /// \ingroup CAT
00005 ///
00006 /// Copyright (c) 2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-23 08:59:02 -0600 (Wed, 23 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //---------------------------------------------------------------------------
00014 
00015 #include "CATMsgThread.h"
00016 
00017 // Thread construction
00018 CATMsgThread::CATMsgThread()
00019 :CATThread()
00020 {
00021     fPollFreq = 0;
00022 }
00023 
00024 // Thread destruction
00025 CATMsgThread::~CATMsgThread()
00026 {
00027     Stop(1000);
00028 }
00029 
00030 // Start a thread. 
00031 bool CATMsgThread::Start( CATUInt32 pollFreq )
00032 {
00033     fPollFreq = pollFreq;
00034     fExitSignal.Reset();
00035     return CATThread::Start(this);    
00036 }
00037 
00038 // Stop a thread, cleanly if possible.
00039 void CATMsgThread::Stop ( CATUInt32 timeout )
00040 {
00041     fExitSignal.Fire();
00042     if (false == WaitStop(timeout))
00043     {
00044         ForceStop();
00045     }
00046 }
00047 
00048 // Called once per timeout
00049 void CATMsgThread::OnThreadIdle()
00050 {
00051     CATTRACE("On thread idle...");
00052 }
00053 
00054 
00055 // Called when messages are received
00056 CATUInt32 CATMsgThread::OnThreadMessage(CATUInt32 msg, CATUInt32 wParam, CATUInt32 lParam)
00057 {    
00058     CATTRACE("On thread msg...");
00059     return 0;
00060 }
00061 
00062 // Posts a message to the thread
00063 bool CATMsgThread::Post(CATUInt32 msg, CATUInt32 wParam, CATUInt32 lParam)
00064 {
00065     if (::PostThreadMessage(fThreadId,msg,wParam,lParam))
00066         return true;
00067 
00068     return false;
00069 }
00070 
00071 void CATMsgThread::ThreadFunction()
00072 {
00073     HANDLE eventHandles[2];
00074     eventHandles[0] = fExitSignal.GetWin32Handle();    
00075     while (MsgWaitForMultipleObjects(1, &eventHandles[0],FALSE, this->fPollFreq,QS_ALLEVENTS) != WAIT_OBJECT_0)
00076     {
00077         MSG msg;
00078         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
00079         { 
00080             // If it is a quit message, exit.
00081             if (msg.message == WM_QUIT)  
00082             {
00083                 fExitSignal.Fire();
00084                 break;
00085             }
00086             else
00087             {
00088                 CATUInt32 result = OnThreadMessage(msg.message, (CATUInt32)msg.lParam, (CATUInt32)msg.wParam);
00089             }
00090         }
00091 
00092         OnThreadIdle();
00093     }
00094 }
00095 

Generated on Mon Feb 11 04:09:45 2008 for Game Accessibility Suite by doxygen 1.5.4