vb.net – 进度条和后台工作者
发布时间:2020-12-17 07:16:27 所属栏目:百科 来源:网络整理
导读:我在VB.Net中有一个进度条和后台工作程序,我希望以不同的形式工作,如下所示: Form1(){MaxRows = 10for i = 0 to MaxRows then// Update my value on the progressbar....next} ProgressBarForm Private Sub ProgressBarForm_Shown(sender As Object,e As Ev
我在VB.Net中有一个进度条和后台工作程序,我希望以不同的形式工作,如下所示:
Form1() { MaxRows = 10 for i = 0 to MaxRows then // Update my value on the progressbar .... next } ProgressBarForm Private Sub ProgressBarForm_Shown(sender As Object,e As EventArgs) Handles Me.Shown TransferProgressBar.Visible = True ProgressBarBackgroundWorker.RunWorkerAsync() End Sub Private Sub ProgressBarBackgroundWorker_DoWork(sender As Object,e As ComponentModel.DoWorkEventArgs) Handles ProgressBarBackgroundWorker.DoWork For i = 0 To TransferProgressBar.Maximum 'Dim Percentage As Integer = Math.Round(((i / (TransferProgressBar.Maximum - TransferProgressBar.Minimum)) * 100)) ProgressBarBackgroundWorker.ReportProgress(i / 100) Next End Sub Private Sub ProgressBarBackgroundWorker_ProgressChanged(sender As Object,e As ComponentModel.ProgressChangedEventArgs) Handles ProgressBarBackgroundWorker.ProgressChanged TransferProgressBar.Value = e.ProgressPercentage PercentageLabel.Text = "Processing....." & TransferProgressBar.Value.ToString() & "%" End Sub Private Sub ProgressBarBackgroundWorker_RunWorkerCompleted(sender As Object,e As ComponentModel.RunWorkerCompletedEventArgs) Handles ProgressBarBackgroundWorker.RunWorkerCompleted MsgBox("Task Completed!") Me.Close() End Sub 如何使用其他表单/ Sub中的backgroundworker更新我的进度条值?请告诉我.我在这里有点困惑. 解决方法
你似乎把它带到了前面.而不是处理ProgressBar更新的BackgroundWorker,使用BGW来完成耗时的工作.然后,您可以定期使用内置的ReportProgress方法为ProgressBar / Form提供更新.
通常,您不能(直接)从其创建的线程(即UI线程)访问UI控件(如ProgressBar). ReportProgress事件在原始/ UI线程上引发,以使基本进度报告变得容易. 但是,它几乎仅限于此.要执行更多操作,例如将长进程的结果发布到ListBox,您可以使用Delegate更新其他/ UI线程上的控件. 具有长时间运行任务的表单 Private WithEvents bgw As New BackgroundWorker Private frmProg As ProgForm ' progress bar form ' start up Private Sub Button1_Click(sender ...etc ' set up bgw.WorkerReportsProgress = True bgw.WorkerSupportsCancellation = True If frmProg Is Nothing Then ' make sure progress form is instanced ProgForm = New frmProg End If If bgw.IsBusy = False Then frmProg.Show() bgw.RunWorkerAsync(10) ' do some important work x10 End If End Sub ' the job that will take a while Private Sub bgw_DoWork(sender As Object,e As DoWorkEventArgs) Handles bgw.DoWork ' ToDo: with multiple workers use sender,not 'bgw' ' get the amount of work to do Dim numToDo As Integer = CInt(e.Argument) ' important,time consuming work done here For n As Integer = 1 To numToDo ' do the "work" System.Threading.Thread.Sleep(100) ' post a notice,passing the percentage (raises the ProgressChanged event) bgw.ReportProgress(Convert.ToInt32((n / numToDo) * 100) Next End Sub ' event raised from DoWork via ReportProgress Private Sub bgw_ProgressChanged(sender As Object,e As ProgressChangedEventArgs) Handles bgw.ProgressChanged ' method added on the Progress form to ' receive percentage and update the meter: frmProg.UpdateProgress(e.ProgressPercentage) ' if the progress bar was on the same form,' update it directly: 'MyProgBar.Value = MyProgBar.Maximum 'MyProgBar.Value = pct End Sub ' optional event raised when the long running task is complete Private Sub bgw_RunWorkerCompleted(sender As Object,e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted ' when all done,report that too MessageBox.Show("Work Complete!") End Sub 请注意,通常使用Thread.Sleep会冻结UI.这不会发生在这里,因为它是BackgroundWorker而不是放入睡眠的UI线程. 带进度条的表单: Public Sub UpdateProgress(pct As Integer) ' ToDo: Add error checking progress.Value = progress.Maximum progress.Value = pct End Sub (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |