“快速修复”是Application.DoEvents()
在每次调用之前添加Sleep()
:
Public Class Form2
Private Const METHOD_COUNT = 4
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Maximum = METHOD_COUNT
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Method_One()
Method_Two()
Method_Three()
Method_Four()
End Sub
Private Sub Method_One()
Label1.Text = "Loading Method One"
ProgressBar1.Value += 1
Application.DoEvents()
Threading.Thread.Sleep(1000)
End Sub
Private Sub Method_Two()
Label1.Text = "Loading Method Two"
ProgressBar1.Value += 1
Application.DoEvents()
Threading.Thread.Sleep(1000)
End Sub
Private Sub Method_Three()
Label1.Text = "Loading Method Three"
ProgressBar1.Value += 1
Application.DoEvents()
Threading.Thread.Sleep(1000)
End Sub
Private Sub Method_Four()
Label1.Text = "Loading Method Four"
ProgressBar1.Value += 1
Application.DoEvents()
Threading.Thread.Sleep(1000)
End Sub
End Class
“正确的修复”是您的“工作”不应该在主 UI 线程中完成,这就是您从按钮单击处理程序调用这些方法所做的事情。相反,您需要将工作移至后台线程,以便 UI 可以自行更新。看看使用BackgroundWorker()控件。您调用它的 ReportProgress() 方法来触发 ProgressChanged() 事件。从该事件中更新 UI 是安全的。当后台线程中的工作完成时,您将收到一个 RunWorkerCompleted() 事件。请注意,如果要使用进度事件,则必须将 WorkerReportsProgress() 属性设置为 True:
Public Class Form2
Private Const METHOD_COUNT = 4
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Maximum = METHOD_COUNT
BackgroundWorker1.WorkerReportsProgress = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not BackgroundWorker1.IsBusy Then
Button1.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Method_One()
Method_Two()
Method_Three()
Method_Four()
End Sub
Private Sub Method_One()
BackgroundWorker1.ReportProgress(1, "Loading Method One")
Threading.Thread.Sleep(1000)
End Sub
Private Sub Method_Two()
BackgroundWorker1.ReportProgress(2, "Loading Method Two")
Threading.Thread.Sleep(1000)
End Sub
Private Sub Method_Three()
BackgroundWorker1.ReportProgress(3, "Loading Method Three")
Threading.Thread.Sleep(1000)
End Sub
Private Sub Method_Four()
BackgroundWorker1.ReportProgress(4, "Loading Method Four")
Threading.Thread.Sleep(1000)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Label1.Text = e.UserState
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Button1.Enabled = True
MessageBox.Show("Done!")
End Sub
End Class