问题描述
- VB.net死循环中使用UI
- 要实现的功能:在死循环中使用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 SubPrivate 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 SubEnd Class问题:为什么我要点击两下Button2,listbox才停止刷数使据?不是点一下就好了嘛?大神们还有什么好的方法,解决死循环中UI不能使用的方法吗?求大神指点!!!C金不多求大神回答啊!
解决方案
Do While True
Application.DoEvents() '加上这个
...
解决方案二:
vb.net中使用多线程
时间: 2024-11-02 11:34:46