平时我们写程序常常会要用到延时函数,一般的做法是用timer控件,要么就是使用sleep函数
这两种方法似乎都有自身的不足之处,这里贴一个老外(牛人)写的延时类,可以做到延时不影响
主程序运行(无负担延时)
类模块源代码如下:
version 1.0 class
begin
multiuse = -1 'true
persistable = 0 'notpersistable
databindingbehavior = 0 'vbnone
datasourcebehavior = 0 'vbnone
mtstransactionmode = 0 'notanmtsobject
end
attribute vb_name = "clswaitabletimer"
attribute vb_globalnamespace = false
attribute vb_creatable = false
attribute vb_predeclaredid = false
attribute vb_exposed = false
option explicit
'**************************************
' name: clswaitabletimer
'
' description: this class encapsulate the waitabletimer api functions to
' put the thread of your application to sleep for a period of time.
' the benefit of a waitable timer to the sleep api is that your
' application will still be responsive to events, where sleep
' will freeze your application for the set interval.
'
' example: 'this is an example for idling your application
' private mobjwaittimer as clswaitabletimer
' private sub runprocess()
' set mobjwaittimer = new clswaitabletimer
' do
' if mbworktodo then
' call processwork()
' else
' mobjwaittimer.wait(5000) 'wait for 5 seconds
' end if
' loop until not mbstop
' set mobjwaittimer = nothing
' end sub
'
' revision history:
private type filetime
dwlowdatetime as long
dwhighdatetime as long
end type
private const wait_abandoned& = &h80&
private const wait_abandoned_0& = &h80&
private const wait_failed& = -1&
private const wait_io_completion& = &hc0&
private const wait_object_0& = 0
private const wait_object_1& = 1
private const wait_timeout& = &h102&
private const infinite = &hffff
private const error_already_exists = 183&
private const qs_hotkey& = &h80
private const qs_key& = &h1
private const qs_mousebutton& = &h4
private const qs_mousemove& = &h2
private const qs_paint& = &h20
private const qs_postmessage& = &h8
private const qs_sendmessage& = &h40
private const qs_timer& = &h10
private const qs_mouse& = (qs_mousemove or qs_mousebutton)
private const qs_input& = (qs_mouse or qs_key)
private const qs_allevents& = (qs_input or qs_postmessage or qs_timer or qs_paint or qs_hotkey)
private const qs_allinput& = (qs_sendmessage or qs_paint or qs_timer or qs_postmessage or qs_mousebutton or qs_mousemove or qs_hotkey or qs_key)
private const units = 4294967296#
private const max_long = -2147483648#