程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> vs2012-VB.net死循環中使用UI

vs2012-VB.net死循環中使用UI

編輯:編程綜合問答
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 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() '加上這個
...

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved