how use CreateTimerQueueTimer()?

i'm trying change my Timer class for see if works better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Timer
{

private:
    static unsigned int TimerCount;
    PHANDLE m_timerHandle=NULL;
    UINT_PTR timerid;
    UINT m_uResolution=0;
    unsigned int TimerID=0;
    unsigned int intInterval=0;

    static void CALLBACK TimerProc(void* InstancePointer, BOOLEAN TimerOrWaitFired)
    {
        Timer* obj=reinterpret_cast<Timer*>(InstancePointer);
        if(obj->timerprocedure!=nullptr)
            obj->timerprocedure();
    }

public:

    std::function<void()> timerprocedure=EmptyEvent;
    Timer(std::function<void()> tmrprocedure=EmptyEvent)
    {
        TimerCount++;
        TimerID=TimerCount-1;
        timerprocedure=tmrprocedure;
    }

    void Stop()
    {
        DeleteTimerQueueTimer(NULL, m_timerHandle, NULL);
        CloseHandle (m_timerHandle);
    }

    unsigned int GetInterval()
    {
        return intInterval;
    }

    void SetInterval(unsigned int uintInterval)
    {
        intInterval = uintInterval;
    }

    property <unsigned int> Interval{GetProperty(Timer::GetInterval),SetProperty(Timer::SetInterval)};

    void Start()
    {
        if(m_timerHandle!=0)
            Stop();
        if (CreateTimerQueueTimer(m_timerHandle,NULL,TimerProc,reinterpret_cast<PVOID>(this),0,(DWORD)intInterval ,WT_EXECUTEINTIMERTHREAD)==0)
            DebugText("error\t" + to_string(GetLastError()));
    }

    ~Timer()
    {
        if(m_timerHandle!=0)
            Stop();
    }
};
unsigned int Timer::TimerCount=0;

compiles and i don't get any error. but i think it's a memory leak.
can anyone tell me where i mistake on CreateTimerQueueTimer()?
"Process terminated with status -1073741819 (0 minute(s), 2 second(s))"
Last edited on
Change line 15:

if(obj->timerprocedure!=nullptr) // Note: do not compare with nullptr

Also note that these type of timer are done with parallel threads. But most gui functions are not thread safe.
theres another problem, i was using more than 16 timers. but we can't.
thanks for all
This limit does not exists:
msdn wrote:
By default, the thread pool has a maximum of 500 threads. To raise this limit, use the WT_SET_MAX_THREADPOOL_THREAD macro defined in WinNT.h.
See:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682485%28v=vs.85%29.aspx
i'm sorry, but is just change the const?
i'm sorry, but these isn't a flag, so i don't know use it :(
doing these:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Start(bool blnPeriodic=true)
    {
        if(m_timerHandle!=NULL)
            Stop();
        ULONG flags;
        DWORD dwPeridic;
        if(blnPeriodic==true)
        {
            flags = WT_EXECUTEDEFAULT;
            dwPeridic=(DWORD)intInterval;
        }
        else
        {
            flags = WT_EXECUTEDEFAULT | WT_EXECUTEONLYONCE;
            dwPeridic=0;
        }

        WT_SET_MAX_THREADPOOL_THREADS(flags,500);
        hTimerQueue = CreateTimerQueue();
        if (CreateTimerQueueTimer(&m_timerHandle,hTimerQueue,Timer::TimerProc,reinterpret_cast<LPVOID>(this),(DWORD)intInterval,dwPeridic ,flags)==FALSE)
            DebugText("error\t" + to_string(GetLastError()));
    }

i can change the number of threads\timers...
and heres my problem on function:
1
2
3
4
5
6
7
8
9
BOOL WINAPI CreateTimerQueueTimer(
  _Out_    PHANDLE             phNewTimer,
  _In_opt_ HANDLE              TimerQueue,
  _In_     WAITORTIMERCALLBACK Callback,
  _In_opt_ PVOID               Parameter,
  _In_     DWORD               DueTime,
  _In_     DWORD               Period,
  _In_     ULONG               Flags
);

i was confusing between DueTime and Period:

1 - DueTime: the interval for the 1st cycle;
2 - Period: the interval for the next cycles... if these is zero, the timer is called only once. if the Period=DueTime, then the cycles works normaly;

thanks too all
Topic archived. No new replies allowed.