【发布时间】:2019-07-29 03:23:51
【问题描述】:
我正在创建一个插入按钮,它将记录从列表视图保存到 mysql 数据库。当用户单击插入按钮时,进度条将显示进度,直到记录被插入。问题是,进度条只有在插入任务完成后才会开始。
这对于vb.net,运行mysql数据库。过去,我尝试过使用倒计时,但它也不起作用。
'load form code
Private Sub formDevice_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Refresh()
Timer2.Enabled = False
End Sub
'insert button
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Try
Timer2.Enable = True
Timer2.Start()
Dim form As New editDevice
form.lblName.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
For Each item As ListViewItem In lvLogs.Items
Dim insert_command As New MySqlCommand("INSERT INTO fingerprint(Enroll_no,Date_time,device_name)" & _
"VALUES (@Enroll_no,@Date_time,@device_name)", connection)
insert_command.Parameters.AddWithValue("@Enroll_no", item.SubItems(1).Text)
insert_command.Parameters.AddWithValue("@Date_time", item.SubItems(4).Text)
insert_command.Parameters.Add("@device_name", MySqlDbType.UInt32).Value = form.lblName.Text
connection.Open()
If insert_command.ExecuteNonQuery() = 1 Then
Else
MessageBox.Show("Error")
End If
connection.Close()
Next
MessageBox.Show("Insert done")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
'timer code
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
ProgressBar1.Increment(10)
If ProgressBar1.Value = 100 Then
Timer2.Stop()
End If
End Sub
我希望单击插入按钮时进度条会运行
【问题讨论】:
-
您的
Click事件处理程序在 UI 线程上执行,因此该线程在该方法完成之前无法执行任何其他操作。这意味着,即使您的Timer引发了它的Tick事件,您的事件处理程序也无法执行,直到您的所有数据访问都已经完成。与往常一样,解决方案是在后台执行工作,即数据访问。你可以Async/Await或BackgroundWorker。还有其他选择,但这是两个主要选择。不,我不会告诉你具体的方法。您需要先研究并尝试一下。
标签: vb.net progress-bar