Sleep或强制循环+DOEVENTS都占CPU100%,有其他的办法吗?谢谢!

解决方案 »

  1.   

    运行程序后,掏一把刀子,对着电脑说,给老子暂停,不然,老子砍烂你,TMD,就OK了。谢谢各位老大捧场
      

  2.   

    这是我利用KENEL32 API写的一个DELAY函数,会用这个函数的人都知道,写游戏做循环就靠它了。
    在机能充许的情况下,精度应该是0。05秒。
    使用时只要加类似Delay(1.05)即可,Option Explicit
    Public Declare Function GetTickCount Lib "kernel32" () As Long
    Const MS_DELAY = 50 '20FPSPublic Function Delay(Times As Single)
    Dim mblnRunning As Boolean
    Dim mlngTimer As Long
    Dim caculate As Single
    mblnRunning = True
    caculate = 0
    Debug.Print Times
    Do While mblnRunning And caculate < Times
        If mlngTimer + MS_DELAY <= GetTickCount() Then
           mlngTimer = GetTickCount()
                 caculate = caculate + 0.05
        End If
        DoEvents 'Important!
    Loop
    End Function
      

  3.   

    用这个,不需要控件,也不需要API,但是只有整数秒的精度
    Public Function pause(seconds As Integer)
        Const SECS_INDAY = 24! * 60 * 60
        Dim start As Single
        start = Timer
        Do: Loop Until (Timer + SECS_INDAY - start) Mod SECS_INDAY >= seconds
    End Function
      

  4.   

    楼上大哥,这个方法我知道啊 可是很占CPU资源啊 没有更好的办法吗?
      

  5.   

    我用的方法 可以非常精确 小于0.01S 但同样用DO……DOEVENTS……LOOP的结构 一样很占CPU资源 没有另一种思路吗?
      

  6.   

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)比如
    Sleep 1  ' to prevent hogging of the CPU
      

  7.   

    Function Delay(ByVal n As Single)
        Dim tm1, tm2 As Single
        tm1 = Timer
        Do
           tm2 = Timer
           If tm2 < tm1 Then tm2 = tm2 + 86400
           If tm2 - tm1 > n Then Exit Do
           DoEvents
        Loop
        
    End Function
      

  8.   

    86400是1天的秒数。晕 这个函数还真打算暂停多长时间啊 呵呵。好象 大家的思路都是用循环或是SLEEP函数。不过在VB里这两种方法都太占CPU资源了----能达到100%,太可怕了。慢点的机器受不了的吧。 没有其他的思路吗?要求是达到使用TIMER控件的效果----不太占用CPU资源的方法。
      

  9.   

    请教楼主你是如何测试出占用CPU资源为100%的?
      

  10.   

    循环等待的时候插入
    sleep 1
    doevents
    是不会占CPU资源的,如果你的CPU占用率还是100%,那么肯定是别的地方造成的。
      

  11.   

    用一简单方法实现!
    在你要暂停的地方弹出模式窗体,styleboder设为1,并设为一象素大小,上面放一timer,就搞了,
    时间到了,就unload
      

  12.   

    干脆设置为ENABLED =FALSE,VISIBLE=FALSE
      

  13.   

    除了do loop,就是timer,好像自己做程序没有太好的办法能做成延时阿
      

  14.   

    to : changyucun(菜猪) 你很吊啊,搞笑。
      

  15.   

    我试了 单纯sleep不占cpu的
    如果你发现sleep占cpu,那么是你其他地方占了CPU
    这样的话,如果你用其他方法暂停,也是要占cpu的
    占cpu的地方可能是你doevents的地方造成的
    你检查其他地方把,光延迟 sleep或者getTickCount都可以的
      

  16.   

    你告诉你的客户换一个两个CPU的机器不就可以了,这样 一个CPU你专门去占用1