无阻塞延迟不卡死窗体代码2

VB.NET · 2023-06-29 · 263 人浏览
    Public Sub 延时秒(ByVal 延时 As Integer, Optional ByVal 单位 As String = "毫秒")

    Dim 延时时间 As Int64                     '因为时间是以100纳秒为单位。
    If 单位 = "毫秒" Then 延时时间 = 延时 * 10000
    If 单位 = "秒" Then 延时时间 = 延时 * 10000 * 1000
    If 单位 = "分" Then 延时时间 = 延时 * 10000 * 1000 * 60
    If 单位 = "时" Then 延时时间 = 延时 * 10000 * 1000 * 60 * 60
    If 单位 = "天" Then 延时时间 = 延时 * 10000 * 1000 * 60 * 60 * 24

    Dim 临时时间 As DateTime = DateTime.Now

    While (DateTime.Now.Ticks - 临时时间.Ticks < 延时时间)
        Application.DoEvents()                              '处理当前在消息队列中的所有 Windows 消息。
    End While
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    While True
        Me.Text = Now
        延时秒(1, "分")
    End While
End Sub