ChighPerformanceTimer::ChighPerformanceTimer(CString p_TimerName,bool p_Periodic,LONG p_Period) { // p_TimerName 定时器名; // p_Periodic 设置是否为周期性定时器; // p_Period时钟周期 g_Valid=false; if (p_Periodic) { g_TimerPeriod=p_Period; g_ManualReset=false; } else { g_TimerPeriod=0; g_ManualReset=true; } g_Set=false; g_TimerExpires.QuadPart=Int32x32To64(-10000,p_Period); memset(g_Name,0,MAX_PATH); if (! p_TimerName.IsEmpty()) if(p_TimerName.GetLength()>MAX_PATH) memcpy(g_Name,p_TimerName,MAX_PATH); else memcpy(g_Name,p_TimerName,p_TimerName.GetLength()); //如果定时器已经创建, 则使用已有的定时器;否则建立一个新的定时器 g_TimerHandle=OpenWaitableTimer(TIMER_ALL_ACCESS | TIMER_MODIFY_STATE | SYNCHRONIZE,TRUE,(char *) g_Name); if (g_TimerHandle==NULL) { //建立并且初始化缺省的安全描述符和属性 g_SecurityAttributes.lpSecurityDescriptor=&g_SecurityDescriptor; InitializeSecurityDescriptor(g_SecurityAttributes.lpSecurityDescriptor,SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(g_SecurityAttributes.lpSecurityDescriptor,TRUE,(PACL)NULL,FALSE); g_SecurityAttributes.nLength = sizeof SECURITY_ATTRIBUTES; g_SecurityAttributes.bInheritHandle=TRUE; g_TimerHandle=CreateWaitableTimer(&g_SecurityAttributes,g_ManualReset,(char *)g_Name); } if(g_TimerHandle==NULL) { //ERROR AND RETURN return; } g_Result=g_WaitableTimer_TickOkay;//????????? g_Valid=true; }
unsigned __stdcall UpdateGlobalDBThread( void* Param ) { #ifdef _SCROUT_ cout << "Update Global DB Thread" << endl; #endif //open timer for updating each hour HANDLE hUpdate = OpenWaitableTimer( TIMER_ALL_ACCESS, NULL, L"ZZVBN-ARARA-12" ); //open timer for retrying if update failed HANDLE hRetry = CreateWaitableTimer( NULL, FALSE, L"ZZVBN-ARARA-24" ); LARGE_INTEGER liDueTime; const int nTimeUnits = 10000000; while( true ) { //wait for global timer WaitForSingleObject( hUpdate, INFINITE ); //wake every hour #ifdef _SCROUT_ cout << "Global Wake!" << endl; #endif while( CCameraObj.lLocal.empty() ) { SwitchToThread(); //if local DB empty - switch to another thread } bool bSuccess = false; do { bSuccess = CCameraObj.Connect( "localhost", 8888 ); //try to connect if( bSuccess ) { auto ptr = CCameraObj.lLocal.begin(); bSuccess = CCameraObj.Send( &*ptr ); // try to send if( bSuccess ) { //remove data sent EnterCriticalSection( &csLocGlob ); CCameraObj.lLocal.erase( ptr ); LeaveCriticalSection( &csLocGlob ); if( !CCameraObj.lLocal.empty() ) { CCameraObj.Disconnect(); } else { break; } } } if( !bSuccess ) { CCameraObj.Disconnect(); liDueTime.QuadPart = -(5/*5 minutes*/ * nTimeUnits); SetWaitableTimer( hRetry, &liDueTime, 0, NULL, NULL, FALSE ); WaitForSingleObject( hRetry, INFINITE ); #ifdef _SCROUT cout << "Local Wake!" << endl; #endif } } while( true ); CCameraObj.Disconnect(); /* PLACEHOLDER */ } return 0; }
// // String-aware overload // TWaitableTimer::TWaitableTimer(const tstring& name, bool inherit, uint32 access) { Handle = OpenWaitableTimer(access, inherit, name.c_str()); if (!Handle) throw TXOwl(_T("OpenWaitableTimer failed.")); }
bool WTimer::Open(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpTimerName) { Close(); m_Timer = OpenWaitableTimer(dwDesiredAccess, bInheritHandle, lpTimerName); return(m_Timer != NULL); }
TWaitableTimer::TWaitableTimer(LPCTSTR name, bool inherit, uint32 access) { Handle = OpenWaitableTimer(access, inherit, name); if (!Handle) throw TXOwl(_T("OpenWaitableTimer failed.")); }