【问题标题】:why does the progress bar not working when button insert is click in vb.net为什么在vb.net中单击按钮插入时进度条不起作用
【发布时间】: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/AwaitBackgroundWorker。还有其他选择,但这是两个主要选择。不,我不会告诉你具体的方法。您需要先研究并尝试一下。

标签: vb.net progress-bar


【解决方案1】:

您可以尝试在 for 循环中添加 Application.DoEvents 调用

        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()
            Application.DoEvents()
        Next

这让您的 UI 更新(以及触发其他事件)在每个处理的项目之间进行一次。

【讨论】:

  • 您是否阅读并理解了您提供的链接中的注意事项?
  • 这只是一种简单的方法来完成他们想要的事情,而无需启动新线程或后台工作人员。这些是更强大的解决方案,但如果您只想在加载应用程序时更新 UI,那可能没问题。
【解决方案2】:

暂时忘记计时器和进度条,让您的数据输入代码顺利运行。

由于您从不显示表单,因此在其上填写标签是没有用的。只需将网格单元格的值赋给参数即可。

将您的数据对象保存在本地,以便确保它们已关闭和处置。即使出现错误,Using...End Using 块也会为您执行此操作。

不要在循环中创建新的命令和参数。每次迭代中唯一变化的是参数中的 2 个值。

不要使用.AddWithValuehttp://www.dbdelta.com/addwithvalue-is-evil/https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ 还有一个: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications

我不得不猜测数据类型和大小。检查数据库的实际类型和大小。

除非您与 MySql 的连接非常缓慢,或者您的列表视图中有大量项目,否则您可能不需要进度条。如果您这样做,请参阅@jmcilhinney 评论。

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
    Try
        Using cn As New MySqlConnection("Your connection string")
            Using cmd As New MySqlCommand("INSERT INTO fingerprint(Enroll_no,Date_time,device_name) VALUES (@Enroll_no,@Date_time,@device_name);", cn)
                cmd.Parameters.Add("@Enroll_no", MySqlDbType.VarChar, 100)
                cmd.Parameters.Add("@Date_time", MySqlDbType.DateTime)
                cmd.Parameters.Add("@device_name", MySqlDbType.UInt32).Value = DataGridView1.CurrentRow.Cells(0).Value.ToString()
                cn.Open()
                Dim d As DateTime
                For Each item As ListViewItem In lvLogs.Items
                    DateTime.TryParse(item.SubItems(4).Text, d)
                    cmd.Parameters("@Enroll_no").Value = item.SubItems(1).Text
                    cmd.Parameters("@Date_time").Value = d
                    cmd.ExecuteNonQuery()
                Next
            End Using
        End Using
        MessageBox.Show("Insert done")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    相关资源
    最近更新 更多