要實現的功能:在死循環中使用VB的UI界面
窗體構成:1個Form(Form1):包含兩個Button(Button1&&Button2)和一個List(LIstBox1)
運行環境:VS2012
VB代碼:
Imports System.Threading
Public Class Form1
Public Declare Function GetTickCount Lib "kernel32" () As Integer
Public Stop_Enable As Boolean
Public n As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Stop_Enable = False
n = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Do While True
If Stop_Enable Then
Exit Do
Else
'hread.Sleep(100)
Wait(100)
ListBox1.Items.Add(n)
ListBox1.TopIndex = ListBox1.Items.Count - 1
n = n + 1
End If
Loop
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Stop_Enable = True Then
Stop_Enable = False
Else
Stop_Enable = True
End If
End Sub
Public Sub Wait(DT As Long)
Static TT As Long
TT = GetTickCount()
Do
Application.DoEvents()
If GetTickCount - TT < 0 Then TT = GetTickCount
Loop Until GetTickCount - TT >= DT
End Sub
Public Sub Form_Paint()
Dim a As Control
For Each a In Me.Controls
a.Invalidate()
Next
End Sub
End Class
問題:為什麼我要點擊兩下Button2,listbox才停止刷數使據?不是點一下就好了嘛?大神們還有什麼好的方法,解決死循環中UI不能使用的方法嗎?
求大神指點!!!
C金不多求大神回答啊!
Do While True
Application.DoEvents() '加上這個
...