With this small class you can create a timer up to 1193 Hours #pragma once #include <string> typedef void (*TIMER_PROC)(UINT idEvent, void* obj); class CTimer { public: enum eTIME_TYPE { eONE_SHOT, ePERIOIDIC }; public: //--CONSTRUCTOR------------------------------------- CTimer(eTIME_TYPE _eType, DWORD _dWaitPeriod, TIMER_PROC _pFunct, LPVOID _pSegment); //--DESTRUCTOR-------------------------------------- virtual ~CTimer(); public: //--PUBLIC METHODS---------------------------------- inline const char* GetErrorMessage(){ return m_szError.c_str(); } UINT Start(); void Stop(); bool SetPeriod(DWORD _dwVal); eTIME_TYPE TimerType() { return m_eTimerType; } UINT GetTimerID(); protected: //--PROTECTED ATTRIBUTES---------------------------- TIMER_PROC m_pCALLBACKFUN; void* m_pSegm; eTIME_TYPE m_eTimerType; DWORD m_WaitPeriod; DWORD m_ThreadID; HANDLE m_hQuitEvent; HANDLE m_hThread; HANDLE m_hAdvisorThread; HANDLE m_hAdvise; HANDLE m_hQuitAdvisor; std::string m_szError; private: //--PRIVATE METHODS--------------------------------- static void TimerThread(void* _pParam); static void AdvisorThread(void* _pParam); };
The implementation //--FILE INCLUDES-------------------------------------------------------- #include <windows.h> #include "Timer.h" //!--CONSTRUCTOR----------------------------------------------------------- // // Method Name: CTimer(..., ...., ...) // /*! Notes Constructor. */ //------------------------------------------------------------------------- CTimer::CTimer(eTIME_TYPE _eType, DWORD _dWaitPeriod, TIMER_PROC _pFunct, LPVOID _pSegment) { m_pCALLBACKFUN = NULL; m_pSegm = NULL; m_eTimerType = _eType; m_WaitPeriod = _dWaitPeriod; if( m_WaitPeriod == 0 ) m_WaitPeriod = 1; m_hThread = m_hAdvisorThread = NULL; m_ThreadID=0; m_pCALLBACKFUN = _pFunct; m_pSegm = _pSegment; m_hQuitEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); m_hQuitAdvisor = ::CreateEvent(NULL, TRUE, FALSE, NULL); m_hAdvise = ::CreateEvent(NULL, FALSE, FALSE, NULL); m_hAdvisorThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)AdvisorThread, this, 0, 0); } //!--DESTRUCTOR------------------------------------------------------------- // // Method Name: ~CTimer() // /*!
Yves
Posts
-
Create really long timers in MFC -
How do i get the HWND for PostMessageI have a main Dialog application wich instantiate a new Dialog Like this... m_pThread= AfxBeginThread(RUNTIME_CLASS(CMultiThread)); CMultiThread is a CWinThread . This one takes care of Showing the new dialog box on the screen BOOL CMultiThread::InitInstance() { CPostDialog Dlg; //CoInitializeEx(0, COINIT_APARTMENTTHREADED); CoInitializeEx(0, COINIT_MULTITHREADED); m_pMainWnd= &Dlg; Dlg.DoModal(); return TRUE; } Everythings is working fine. On the ExitInstance() of this thread i want to post a message to the caller (Main Dialog) that CPostDialog is now over. The problem is no matter what i try i can't get the HWND of the caller. I think that i should get the HWND in the InitInstance What i did so far in the main dialog is to declare a public variable like this HWND m_LocalWnd; Then on the OnInitDialog() of the main dialog m_LocalWnd =m_hWnd; Then i change the InitInstance of my CWinThread like this BOOL CMultiThread::InitInstance() { CMessageTestDlg pApp; m_HwndSource = pApp.m_hWnd; CPostDialog Dlg; //CoInitializeEx(0, COINIT_APARTMENTTHREADED); CoInitializeEx(0, COINIT_MULTITHREADED); m_pMainWnd= &Dlg; Dlg.DoModal(); return TRUE; } CMessageTestDlg is already running and i should be able to get the HWND.. but it always return 0 What is it wrong. Yves Lessard