SetTimer和KillTimer函数

在前面介绍了一对定时器的API函数使用,现在又介绍另外一对API函数的使用。它使用起来比前的函数要简单一些,但它一般是使用到有窗口的程序里,并且它的精度也没有前面的API函数高,对于一些要求不高的场合还是非常合适的。它是采用消息通知的方式,每当定时到了就会收到一条消息。

函数SetTimer和KillTimer声明如下:

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

// C#

[DllImport("user32.dll", ExactSpelling=true)]
static extern IntPtr SetTimer(IntPtr hWnd, IntPtr nIDEvent, uint uElapse, TimerProc lpTimerFunc);
delegate void TimerProc(IntPtr hWnd, uint uMsg, IntPtr nIDEvent, uint dwTime);

// or alternatively
[DllImport("user32.dll", ExactSpelling=true)]
static extern IntPtr SetTimer(IntPtr hWnd, IntPtr nIDEvent, uint uElapse, IntPtr lpTimerFunc);

[DllImport("user32.dll", ExactSpelling=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool KillTimer(IntPtr hWnd, IntPtr uIDEvent);

// VB
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function SetTimer _
(ByVal hWnd As IntPtr, ByVal nIDEvent As IntPtr, ByVal uElapse As UInteger, ByVal lpTimerFunc As IntPtr) As IntPtr
End Function

// or alternatively

<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function SetTimer _
(ByVal hWnd As IntPtr, ByVal nIDEvent As IntPtr, ByVal uElapse As UInteger, ByVal lpTimerFunc As TimerProc) As IntPtr
End Function

Public Delegate Sub TimerProc(ByVal hWnd As IntPtr, ByVal uMsg As UInteger, ByVal nIDEvent As IntPtr, ByVal dwTime As UInteger)

<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function KillTimer(ByVal hWnd As IntPtr, ByVal nIDEvent As IntPtr) As Boolean
End Function

hWnd是窗口接收定时器的句柄。

nIDEvent是定时器事件标识号。

uElapse是定时器的毫秒值。

lpTimerFunc是定时到达回调函数。

调用函数的例子如下:

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

// Set two timers.

SetTimer(hwnd, // handle to main window
IDT_TIMER1, // timer identifier
10000, // 10-second interval
(TIMERPROC) NULL); // no timer callback

SetTimer(hwnd, // handle to main window
IDT_TIMER2, // timer identifier
300000, // five-minute interval
(TIMERPROC) NULL); // no timer callback

case WM_TIMER:

switch (wParam)
{
case IDT_TIMER1:
// process the 10-second timer

return 0;

case IDT_TIMER2:
// process the five-minute timer

return 0;
}

// Set the timer.

SetTimer(hwnd, // handle to main window
IDT_TIMER3, // timer identifier
5000, // 5-second interval
(TIMERPROC) MyTimerProc); // timer callback

HWND hwndTimer; // handle to window for timer messages
MSG msg; // message structure

while (GetMessage(&msg, // message structure
NULL, // handle to window to receive the message
0, // lowest message to examine
0)) // highest message to examine
{

// Post WM_TIMER messages to the hwndTimer procedure.

if (msg.message == WM_TIMER)
{
msg.hwnd = hwndTimer;
}

TranslateMessage(&msg); // translates virtual-key codes
DispatchMessage(&msg); // dispatches message to window
}

// Destroy the timers.

KillTimer(hwnd, IDT_TIMER1);
KillTimer(hwnd, IDT_TIMER2);
KillTimer(hwnd, IDT_TIMER3);