设置定时器(NULL,NULL,UINT,NULL)
SetTimer(NULL, NULL, UINT, NULL)
我正在审查具有以下代码行的 MFC 应用程序:
SetTimer(NULL, NULL, 50, NULL);
来自 WinAPI 文档:
UINT_PTR SetTimer(
HWND hWnd,
UINT_PTR nIDEvent,
UINT uElapse,
TIMERPROC lpTimerFunc
);
Parameters
hWnd
Type: HWND
A handle to the window to be associated with the timer. This window
must be owned by the calling thread. If a NULL value for hWnd is
passed in along with an nIDEvent of an existing timer, that timer will
be replaced in the same way that an existing non-NULL hWnd timer will
be.
nIDEvent
Type: UINT_PTR
A nonzero timer identifier. If the hWnd parameter is NULL, and the
nIDEvent does not match an existing timer then it is ignored and a new
timer ID is generated. If the hWnd parameter is not NULL and the
window specified by hWnd already has a timer with the value nIDEvent,
then the existing timer is replaced by the new timer. When SetTimer
replaces a timer, the timer is reset. Therefore, a message will be
sent after the current time-out value elapses, but the previously set
time-out value is ignored. If the call is not intended to replace an
existing timer, nIDEvent should be 0 if the hWnd is NULL.
我认为这行代码的目的是创建一个 50ms 计时器(而不是替换某些现有计时器),但我不明白 hWnd 参数为 NULL 的效果。
当我使用 NULL 的 hWnd 参数和 NULL 的 nIDEvent 参数调用 SetTimer 时,文档没有向我说明会发生什么。
问题:
hWnd == NULL 在 SetTimer 中有什么作用?
nIDEvent == NULL 是否总是暗示我想创建一个新计时器(其 ID 将由调用 return 编辑),而不是 'replace' 现有的定时器?
我们是否应该从 SetTimer 中捕获 return 值,并在我们退出该线程时获得相应的 KillTimer(return_value)
?
SetTimer中的hWnd == NULL有什么作用?
当 hWnd
为 NULL
时,计时器未与 window 相关联。将其视为线程或应用程序中的全局独立计时器。
nIDEvent == NULL是否总是暗示我要创建一个新的定时器
(其 ID 将由调用 return 编辑),而不是 'replace' 现有的
定时器?
是的。请看这一行:
If the call is not intended to replace an existing timer, nIDEvent
should be 0 if the hWnd is NULL.
我们是否应该从 SetTimer 中捕获 return 值,并在退出该线程时有一个相应的 KillTimer(return_value)?
是的。请看这部分:
If the function succeeds and the hWnd parameter is NULL, the return
value is an integer identifying the new timer. An application can pass
this value to the KillTimer function to destroy the timer.
正确的代码是:
UINT_PTR timerId = SetTimer(NULL, 0, 50, NULL);
// some code
KillTimer(NULL, timerId);
我正在审查具有以下代码行的 MFC 应用程序:
SetTimer(NULL, NULL, 50, NULL);
来自 WinAPI 文档:
UINT_PTR SetTimer(
HWND hWnd,
UINT_PTR nIDEvent,
UINT uElapse,
TIMERPROC lpTimerFunc
);
Parameters
hWnd
Type: HWND
A handle to the window to be associated with the timer. This window must be owned by the calling thread. If a NULL value for hWnd is passed in along with an nIDEvent of an existing timer, that timer will be replaced in the same way that an existing non-NULL hWnd timer will be.
nIDEvent
Type: UINT_PTR
A nonzero timer identifier. If the hWnd parameter is NULL, and the nIDEvent does not match an existing timer then it is ignored and a new timer ID is generated. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL.
我认为这行代码的目的是创建一个 50ms 计时器(而不是替换某些现有计时器),但我不明白 hWnd 参数为 NULL 的效果。
当我使用 NULL 的 hWnd 参数和 NULL 的 nIDEvent 参数调用 SetTimer 时,文档没有向我说明会发生什么。
问题:
hWnd == NULL 在 SetTimer 中有什么作用?
nIDEvent == NULL 是否总是暗示我想创建一个新计时器(其 ID 将由调用 return 编辑),而不是 'replace' 现有的定时器?
我们是否应该从 SetTimer 中捕获 return 值,并在我们退出该线程时获得相应的
KillTimer(return_value)
?
SetTimer中的hWnd == NULL有什么作用?
当 hWnd
为 NULL
时,计时器未与 window 相关联。将其视为线程或应用程序中的全局独立计时器。
nIDEvent == NULL是否总是暗示我要创建一个新的定时器 (其 ID 将由调用 return 编辑),而不是 'replace' 现有的 定时器?
是的。请看这一行:
If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL.
我们是否应该从 SetTimer 中捕获 return 值,并在退出该线程时有一个相应的 KillTimer(return_value)?
是的。请看这部分:
If the function succeeds and the hWnd parameter is NULL, the return value is an integer identifying the new timer. An application can pass this value to the KillTimer function to destroy the timer.
正确的代码是:
UINT_PTR timerId = SetTimer(NULL, 0, 50, NULL);
// some code
KillTimer(NULL, timerId);