vb.net 带进程的进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21283382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Progress Bar with process
提问by Leo Elvin Lee
When i click my command button it takes a few seconds to complete the process which could make the end users think the program is lagging, then i thought of the progress bar. so how can make the progress bar work while the process is running so that the users will not think that the program is lagging, i'm new to vb.net, i research quiet some time now and still can't get how it works nor how will i start.
当我单击我的命令按钮时,完成该过程需要几秒钟,这可能会使最终用户认为程序滞后,然后我想到了进度条。那么如何在进程运行时使进度条工作,以便用户不会认为程序滞后,我是 vb.net 的新手,我现在安静地研究了一段时间,但仍然无法了解它是如何工作的我将如何开始。
thanks for the help in advance
我在这里先向您的帮助表示感谢
this is my command button process.
这是我的命令按钮过程。
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
回答by Matt Wilko
You should perform your long running process in a separate thread or background worker.
您应该在单独的线程或后台工作程序中执行长时间运行的进程。
Here is how to use the background worker:
下面是如何使用后台工作者:
Add a BackgroundWorker and ProgressBar to your Form
将 BackgroundWorker 和 ProgressBar 添加到您的表单
Add a private variable to hold your result
添加一个私有变量来保存你的结果
Private _success As Boolean
Add code to show the ProgressBar and Start the worker in Form_Load
添加代码以显示 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
Perform your long running operation here
在此处执行您的长时间运行操作
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
When the worker has finished hide the progress bar and depending on the result perform some action
当工人完成隐藏进度条并根据结果执行一些操作
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

