开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 2971|回复: 4
收起左侧

[其它求助] VB里有没有不占用CPU的超级延迟?

[复制链接]
结帖率:21% (3/14)
发表于 2011-11-13 16:28:59 | 显示全部楼层 |阅读模式   江苏省苏州市
{:soso__12237759026638490920_4:}求助。。。。。。。。

回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值无答案申请取消悬赏投诉有答案未采纳为最佳

结帖率:100% (2/2)
发表于 2011-11-13 16:38:58 | 显示全部楼层   广东省中山市
本帖最后由 易团颜 于 2011-11-13 16:53 编辑
  1. Option Explicit

  2. Private Type FILETIME  
  3.     dwLowDateTime As Long
  4.     dwHighDateTime As Long
  5. End Type

  6. Private Const WAIT_ABANDONED& = &H80&
  7. Private Const WAIT_ABANDONED_0& = &H80&
  8. Private Const WAIT_FAILED& = -1&
  9. Private Const WAIT_IO_COMPLETION& = &HC0&
  10. Private Const WAIT_OBJECT_0& = 0
  11. Private Const WAIT_OBJECT_1& = 1
  12. Private Const WAIT_TIMEOUT& = &H102&
  13. Private Const INFINITE = &HFFFF
  14. Private Const ERROR_ALREADY_EXISTS = 183&
  15. Private Const QS_HOTKEY& = &H80
  16. Private Const QS_KEY& = &H1
  17. Private Const QS_MOUSEBUTTON& = &H4
  18. Private Const QS_MOUSEMOVE& = &H2
  19. Private Const QS_PAINT& = &H20
  20. Private Const QS_POSTMESSAGE& = &H8
  21. Private Const QS_SENDMESSAGE& = &H40
  22. Private Const QS_TIMER& = &H10
  23. Private Const QS_MOUSE& = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
  24. Private Const QS_INPUT& = (QS_MOUSE Or QS_KEY)
  25. Private Const QS_ALLEVENTS& = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)
  26. 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)
  27.   
  28. Private Const Units = 4294967296#
  29. Private Const MAX_LONG = -2147483648#
  30. Private Declare Function CreateWaitableTimer Lib "kernel32" Alias "CreateWaitableTimerA" (ByVal lpSemaphoreAttributes As Long, ByVal bManualReset As Long, ByVal lpName As String) As Long
  31. Private Declare Function OpenWaitableTimer Lib "kernel32" Alias "OpenWaitableTimerA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
  32. Private Declare Function SetWaitableTimer Lib "kernel32" (ByVal hTimer As Long, lpDueTime As FILETIME, ByVal lPeriod As Long, ByVal pfnCompletionRoutine As Long, ByVal lpArgToCompletionRoutine As Long, ByVal fResume As Long) As Long
  33. Private Declare Function CancelWaitableTimer Lib "kernel32" (ByVal hTimer As Long)
  34. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  35. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  36. Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Long, pHandles As Long, ByVal fWaitAll As Long, ByVal dwMilliseconds As Long, ByVal dwWakeMask As Long) As Long
  37. Private mlTimer As Long

  38. Private Sub Class_Terminate()
  39.     On Error Resume Next
  40.     If mlTimer <> 0 Then CloseHandle mlTimer
  41. End Sub

  42. Public Sub Wait(MilliSeconds As Long)
  43.     On Error GoTo ErrHandler
  44.     Dim ft As FILETIME
  45.     Dim lBusy As Long
  46.     Dim lRet As Long
  47.     Dim dblDelay As Double
  48.     Dim dblDelayLow As Double

  49. mlTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer" & Format$(Now(), "NNSS"))

  50. If Err.LastDllError <> ERROR_ALREADY_EXISTS Then
  51.         ft.dwLowDateTime = -1
  52.         ft.dwHighDateTime = -1
  53.         lRet = SetWaitableTimer(mlTimer, ft, 0, 0, 0, 0)
  54.     End If

  55. 'Convert the Units to nanoseconds.
  56.     dblDelay = CDbl(MilliSeconds) * 10000#

  57. 'By setting the high/low time to a negative number, it tells
  58.     'the Wait(in SetWaitableTimer) to use an offset time as/
  59.     'opposed to a hardcoded time. If it were positive, it would
  60.     'try to convert the value to GMT.
  61.     ft.dwHighDateTime = -CLng(dblDelay / Units) - 1
  62.     dblDelayLow = -Units * (dblDelay / Units - Fix(CStr(dblDelay / Units)))

  63. If dblDelayLow < MAX_LONG Then dblDelayLow = Units + dblDelayLow

  64. ft.dwLowDateTime = CLng(dblDelayLow)
  65. lRet = SetWaitableTimer(mlTimer, ft, 0, 0, 0, False)

  66. Do

  67. 'QS_ALLINPUT means that MsgWaitForMultipleObjects will
  68. 'return every time the thread in which it is running gets
  69. 'a message. If you wanted to handle messages in here you could,
  70. 'but by calling Doevents you are letting DefWindowProc
  71. 'do its normal windows message handling---Like DDE, etc.
  72. lBusy = MsgWaitForMultipleObjects(1, mlTimer, False, INFINITE, QS_ALLINPUT&)
  73. DoEvents
  74. Loop Until lBusy = WAIT_OBJECT_0

  75. 'Close the handles when you are done with them.
  76.     CloseHandle mlTimer
  77.     mlTimer = 0
  78. Exit Sub
  79. ErrHandler:
  80. Err.Raise Err.Number, Err.Source, "[clsWaitableTimer.Wait]" & Err.Description
  81. End Sub
复制代码

回复 支持 反对

使用道具 举报

结帖率:23% (3/13)
发表于 2011-11-17 23:35:41 | 显示全部楼层   广东省深圳市
就一个超级延迟而已,居然要这么长的代码,vb,汗颜
回复 支持 反对

使用道具 举报

发表于 2011-11-28 03:36:16 | 显示全部楼层   美国
99无敌 发表于 2011-11-17 23:35
就一个超级延迟而已,居然要这么长的代码,vb,汗颜

2楼误导新人啊。。= =想多了,只要加个Doevent就行
回复 支持 反对

使用道具 举报

发表于 2012-11-9 16:48:18 | 显示全部楼层   广西壮族自治区贵港市
这个Doevent好!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表