-1

对不起我的英语不好

  1. 当用户单击“开始”按钮时,我试图显示控件中Phone列中的所有记录。Textbox1我希望看到所有这些记录都传入Textbox1While 它正在处理For循环。但它目前的处理速度非常快,所以我只能看到Textbox1. 我怎么了?

  2. 在处理For循环时,我将“开始”按钮更改为“停止”按钮。因此,当我单击按钮并且它当前的Text值等于 时"Stop",我希望它跳过For循环并将值传递TextBox1给 my FirstWin。然后它应该将BtnStart.Text背面更改为"Start"

这是我的代码:

Public Class PhoneFortune
    Dim CN As OleDbConnection
    Dim CM As OleDbCommand
    Dim DA As OleDbDataAdapter
    Dim DT As New DataTable

    Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
        If BtnStart.Text = "Start" Then
            CM = New OleDbCommand("SELECT * FROM TblPhoneNumber", CN)
            DA = New OleDbDataAdapter(CM)
            DA.Fill(DT)
            For i = 0 To DT.Rows.Count - 1
                TextBox1.Text = DT.Rows(i)("Phone")
            Next
            BtnStart.Text = "Stop"
        End If
        If BtnStart.Text = "Stop" Then
            If FirstWin.Text = "" Then
                FirstWin.Text = TextBox1.Text
                BtnStart.Text = "Start"
            ElseIf SecondWin.Text = "" Then
                SecondWin.Text = TextBox1.Text
                BtnStart.Text = "Start"
            ElseIf ThirdWin.Text = "" Then
                ThirdWin.Text = TextBox1.Text
                BtnStart.Text = "Start"
            End If
        End If
    End Sub

    Private Sub PhoneFortune_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CN = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\db.accdb;Jet OLEDB:Database Password=12345;")
        CN.Open()
    End Sub
End Class
4

3 回答 3

0

我希望我们在同一个页面上,但是如果你想稍微减慢这个过程,你可以在 FOR 循环中使用这两行代码,
System.Threading.Thread.Sleep(1000)
Me.Refresh()其中 1000 是一秒(值以毫秒表示)。

现在那个FirstWin.Text=""等是用来做什么的?您是否尝试开始循环,然后将文本更改为“停止”,以便当用户单击停止时它会在当前记录处停止?转到 youtube 并寻找多线程视频,不知道如何帮助你希望其余的帮助。如果这有用请承认

于 2013-09-04T20:22:00.997 回答
0

对于像这样简单的事情,System.Threading.Thread.Sleep(100)(阻止线程)然后Application.DoEvents()(处理任何按钮点击)似乎可以解决问题。

对于更复杂的东西,这种做法会导致重入和难以重现的bug;在那种情况下,你肯定需要异步编程

于 2013-09-06T05:57:23.467 回答
0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Button1.Text = "Start" Then
                If TextBox1.Text = "" Then
                    t1 = New Thread(AddressOf Me.PhoneThread)
                    t1.Start()
                    Button1.Text = "Stop"
                Else
                    t1.Resume()
                    Button1.Text = "Stop"
                End If
            Else    'Click Stop
                t1.Suspend()
                Button1.Text = "Start"
                If FirstWin.Text = "" Then
                    FirstWin.Text = TextBox1.Text
                ElseIf SecondWin.Text = "" Then
                    SecondWin.Text = TextBox1.Text
                ElseIf ThirdWin.Text = "" Then
                    ThirdWin.Text = TextBox1.Text
                End If


            End If
        End Sub
        Sub PhoneThread()
            'Dim ThreadsArray As List(Of Threading.Thread) = New List(Of Threading.Thread)
            Dim s As Integer
            'MsgBox(DT.Rows.Count)
            For s = 0 To DT.Rows.Count
                If s = DT.Rows.Count Then
                    s = 0
                Else
                    TextBox1.Text = DT.Rows(s)("Phone")
                    '   ThreadsArray.Add(t1)
                    Thread.Sleep(19)
                    'TextBox1.Text = s.ToString()
                    Me.Refresh()
                End If
            Next
        End Sub
 Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
        Me.Close()

    End Sub
于 2013-09-05T20:29:57.937 回答