VB下的幾個延時程式

來源:互聯網
上載者:User
 最近寫個東西用到延時函數,整理一下:
代碼1Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)代碼2Private Declare Function timeGetTime Lib "winmm.dll" () As LongFunction Delaytime(Milliseconds As Integer)    Dim Savetime As Double    Savetime = timeGetTime    '記下開始時的時間    While timeGetTime < Savetime + Milliseconds    '迴圈等待        DoEvents      '轉讓控制權,以便讓作業系統處理其它的事件。    WendEnd Function代碼3Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Private Declare Function timeGetTime Lib "winmm.dll" () As LongFunction Delaytime(Milliseconds As Integer)   '改造後的延時程式    Dim Savetime As Double    Savetime = timeGetTime    '記下開始時的時間    While timeGetTime < Savetime + Milliseconds    '迴圈等待        Sleep (30)    '加上這麼一句後,該函數延時不佔用CPU資源,也不像sleep函數,調用後程式無反應了。        DoEvents      '轉讓控制權,以便讓作業系統處理其它的事件。    WendEnd Function

以上三個代碼都能延時,代碼一的缺點是延時間長度點後,在延時過程中程式無反應了;代碼二的缺點是延時的時候程式有反應,延時過程中CPU接近100%,代碼三是結合前兩個代碼,延時過程中程式也有反應,CPU佔用也小。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.