4

I want to set a timer for a window, but I don't know if that window already has timers running. If it does I do not want to overwrite any of those, so I need to choose a timer id that is free. MSDN is not helpful in my case, they describe pretty much all permutations EXCEPT if you have a hwnd and not want to replace a timer.

So either if it's possible to give 0 or something as nIDEvent and have Windows create a new uniqe id automatically, or if there is some way to list currently running timers so I can avoid using any of those?

MSDN for SetTimer: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906%28v=vs.85%29.aspx

DaedalusAlpha
  • 1,610
  • 4
  • 20
  • 33

1 Answers1

-1

As @Jonathan Potter said, you can write something like:

VOID CALLBACK TimerCallback(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
    BOOL bRetKill = ::KillTimer(NULL, idEvent);
    // do something
}

void CSomeModel::StartTimer()
{ 
    UINT_PTR hSetTimer = ::SetTimer(NULL, 0, 1000, TimerCallback);
}

every time ::SetTimer is called, the idEvent ID will be unique.

sunpochin
  • 328
  • 1
  • 4
  • 17
  • 2
    Keep in mind that thread timers will not work when the thread is blocked waiting for a pop-up menu of message box to be closed. Whereas timers associated to the owner window will work just fine. – GetFree Dec 21 '17 at 08:08