要实现的功能:在死循环中使用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金不多求大神回答啊!