进度条与过程

时间:2014-01-22 12:47:11

标签: vb.net progress-bar

当我单击我的命令按钮时,需要几秒钟才能完成该过程,这可能会让最终用户认为程序滞后,然后我想到了进度条。那么如何在进程运行时让进度条工作,以便用户不会认为程序滞后,我是vb.net的新手,我现在研究安静一段时间仍然无法得到它是如何工作的也不会如何开始。

感谢您的帮助

这是我的命令按钮过程。

        Private Sub LoginBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginBTN.Click
    Using ClientDashboardx As New ClientDashboard
        Try

            cn = New ADODB.Connection
            conDB()
            cn.Open()

            rs = New ADODB.Recordset
            rs.Open("SELECT * FROM tb_registration", cn, 0, 3)

            If txtuserid.Text = "" Or txtuserpass.Text = "" Then
                MsgBox("Value Null")
                Exit Sub
            Else
                Do While Not rs.EOF
                    Dim strLogin = txtin.Text
                    Dim strLogout = txtout.Text
                    If rs("st_acc_number").Value = txtuserid.Text And rs("st_password").Value = txtuserpass.Text And rs("st_log").Value = strLogin Then
                        MsgBox("Account in use")
                        Exit Sub
                    ElseIf rs("st_acc_number").Value = txtuserid.Text And rs("st_password").Value = txtuserpass.Text And rs("st_log").Value = strLogout Then
                        Dim strID = rs("st_acc_number").Value
                        Dim strUserName = rs("st_fname").Value & " " & rs("st_lname").Value
                        Dim strTotalTime = rs("st_totaltimeleft").Value
                        rs.Close()
                        SaveAccNumber.Text = strID
                        SaveUserName.Text = strUserName
                        SaveTotalTime.Text = strTotalTime

                        cn.BeginTrans()
                        cn.Execute("UPDATE tb_registration SET st_log='in' where st_acc_number='" & SaveAccNumber.Text & "'")
                        cn.CommitTrans()
                        cn.Close()

                        ClientDashboardx.id_lbl.Text = SaveAccNumber.Text
                        ClientDashboardx.iduser_lbl.Text = SaveUserName.Text
                        ClientDashboardx.UserTotalTime.Text = SaveTotalTime.Text
                        ClientDashboardx.AutoUpdate_Button.Enabled = True
                        Me.Hide()
                        ClientDashboardx.ShowDialog()
                        Me.Show()
                        Exit Sub
                    Else
                        rs.MoveNext()
                    End If
                Loop
                MsgBox("User/Pass Mismatch")
                Exit Sub
            End If
        Catch ex As Exception
            MsgBox("Server Offline", MsgBoxStyle.Critical, "")
        End Try
    End Using
End Sub

1 个答案:

答案 0 :(得分:3)

您应该在单独的帖子或background worker中执行长时间运行的过程。

以下是如何使用后台工作者:

将BackgroundWorker和ProgressBar添加到表单

添加私有变量以保存结果

Private _success As Boolean

添加代码以显示ProgressBar并在Form_Load中启动工作程序

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _success = False
    ProgressBar1.Style = ProgressBarStyle.Marquee
    ProgressBar1.Visible = True
    BackgroundWorker1.RunWorkerAsync()
End Sub

在这里执行长时间运行

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    'perform your db access here and set the result in _success
End Sub

当工人完成后隐藏进度条并根据结果执行一些操作

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    ProgressBar1.Visible = False
    If _success Then
        Me.Hide()
        ClientDashboardx.ShowDialog()
        Me.Show()
    Else
        MsgBox("User/Pass Mismatch")
    End If
End Sub