开发者

multiple ::setTimer in c++

开发者 https://www.devze.com 2023-03-15 18:05 出处:网络
I need more than开发者_JAVA技巧 one timer using the ::SetTimer function having the same EventID. Is this possible/a good idea to create multiple window handles with ::CreateWindow, and then assign eac

I need more than开发者_JAVA技巧 one timer using the ::SetTimer function having the same EventID. Is this possible/a good idea to create multiple window handles with ::CreateWindow, and then assign each timer to the created window handles? Any other choices?


Edit: Anyway, I only want to know what happens here:

int eventID = 0;
hWnd1 = ::CreateWindow(...);
hWnd2 = ::CreateWindow(...);
::SetTimer(hWnd1, eventID, ...);
::SetTimer(hWnd2, eventID, ...);

Does every hWnd has it's own eventID list? and do they get handled independently? I don't want to discuss whether this design makes sense or not..


From http://msdn.microsoft.com/en-us/library/ms644906%28v=vs.85%29.aspx

The timer identifier, nIDEvent, is specific to the associated window. Another window can have its own timer which has the same identifier as a timer owned by another window. The timers are distinct.


If you are using the same EventID you should be able to make do with the same timer but at different intervals. You can simply kill the old one with KillTimer() then create another one with SetTimer() which triggers more or less often. If you have a situation where this isn't possible then quite likely there is a flaw in the design of your program.

Consider using different EventIDs and give us some more info about why you feel doing it this way is necessary. You most likely don't want to go about creating random windows if they are only going to serve the purpose of having timer messages registering to them. This sounds like a messy hack which will yield unnecessary computation and inefficient overhead.

Of course a timer does not actually require you to associate a hWnd with it. The hWnd is optional and you can instead associate your timer with a callback ( lpTimerFunc ).


Do you really want two windows? You can set two timers in one window. You can identify which timer is fired, using the the ID parameter of WM_TIMER handler (OnTimer() in MFC). Please consider this way if you're not really required high precision timer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号