now works fine:
ULONG CreateTimerQueueTimerWT_SET_MAX_THREADPOOL_THREADS(ULONG Flags=0, ULONG Limit=500)
{
return ((Flags)|=(Limit)<<16);
}
class Timer
{
private:
static unsigned int TimerCount;
HANDLE m_timerHandle=NULL;
UINT_PTR timerid;
UINT m_uResolution=0;
unsigned int TimerID=0;
unsigned int intInterval=0;
HANDLE hTimerQueue=NULL;
HANDLE gDoneEvent= CreateEvent(NULL, TRUE, FALSE, NULL);
static void CALLBACK TimerProc(PVOID InstancePointer, BOOLEAN TimerOrWaitFired)
{
Timer* obj=reinterpret_cast<Timer*>(InstancePointer);
if(obj->timerprocedure)
{
obj->timerprocedure();
SetEvent(obj->gDoneEvent);
}
}
public:
std::function<void()> timerprocedure=EmptyEvent;
Timer(std::function<void()> tmrprocedure=EmptyEvent)
{
TimerCount++;
TimerID=TimerCount-1;
timerprocedure=tmrprocedure;
}
void Stop()
{
WaitForSingleObject(&gDoneEvent,INFINITE);
DeleteTimerQueueTimer(hTimerQueue, m_timerHandle, NULL);
CloseHandle (m_timerHandle);
DeleteTimerQueue(hTimerQueue);
m_timerHandle=NULL;
}
unsigned int GetInterval()
{
return intInterval;
}
void SetInterval(unsigned int uintInterval)
{
intInterval = uintInterval;
}
property <unsigned int> Interval{GetProperty(Timer::GetInterval),SetProperty(Timer::SetInterval)};
void Start(bool blnPeriodic=true)
{
if(m_timerHandle!=NULL)
Stop();
ULONG flags;
DWORD dwPeridic;
if(blnPeriodic==true)
{
flags=CreateTimerQueueTimerWT_SET_MAX_THREADPOOL_THREADS(WT_EXECUTEDEFAULT,500);
dwPeridic=(DWORD)intInterval;
}
else
{
flags=CreateTimerQueueTimerWT_SET_MAX_THREADPOOL_THREADS(WT_EXECUTEDEFAULT | WT_EXECUTEONLYONCE,500);
dwPeridic=0;
}
hTimerQueue = CreateTimerQueue();
if (CreateTimerQueueTimer(&m_timerHandle,hTimerQueue,Timer::TimerProc,reinterpret_cast<LPVOID>(this),(DWORD)intInterval,dwPeridic ,flags)==FALSE)
DebugText("error\t" + to_string(GetLastError()));
}
~Timer()
{
if(m_timerHandle!=NULL)
Stop();
TimerCount=TimerCount-1;
}
};
unsigned int Timer::TimerCount=0;
now it's prepared for once.
please i need 2 corrections:
1 - everytime that i use a Multithread's or a Timer's, i must use Wait functions with CreateEvent() and SetEvent()?
2 - i don't have sure, but seems that i can control the number of timers, but for more than 10ms, it's the best use the SetTimer() and KillTimer(), or i use the CreateTimerQueueTimer() for both?
i must use the CreateTimerQueueTimerWT_SET_MAX_THREADPOOL_THREADS() or i will get errors on lValue, like i said before.
thanks for all to all
thank you