DoEvents!?

今天,突然有朋友問我,VB6 上頭好用的 DoEvents 在 VB.Net 上似乎不見了。這是怎麼一回事呢?
其實,並不是 VB.Net 不再提供,而是被分派到 Application 這個 Class 上了。正確的引用方法為 Application.DoEvents()
DoEvents 的主要功能是為了讓 Windows 能回應多個(幾乎)同時發生的事件。

程式碼範例。試著把 Application.DoEvents() Rem 起來,就會輕易發現差別在哪裡了。

'函數名稱: DelayTime
'功能說明: 延遲時間
'參數說明: intSecond(欲延遲之秒數)
Friend Sub DelayTime(ByVal intSecond As Integer)
 Dim lnTime1 As Long, lnTime2 As Long
 lnTime1 = Stopwatch.GetTimestamp \ Stopwatch.Frequency

 While lnTime2 - lnTime1 < intSecond
  lnTime2 = Stopwatch.GetTimestamp \ Stopwatch.Frequency
  Application.DoEvents()
 End While

 Exit Sub
End Sub

0 留言