Vb.net 如何动态计算后台工作进程中的进度条值?
如何基于gridview总行数动态计算backgound worker中的进度条值?BackgroundWorker在与UI线程不同的线程上运行。因此,如果试图从后台工作程序的事件处理程序方法中修改窗体上的任何控件,则会出现异常 要更新窗体上的控件,有两个选项:Vb.net 如何动态计算后台工作进程中的进度条值?,vb.net,winforms,Vb.net,Winforms,如何基于gridview总行数动态计算backgound worker中的进度条值?BackgroundWorker在与UI线程不同的线程上运行。因此,如果试图从后台工作程序的事件处理程序方法中修改窗体上的任何控件,则会出现异常 要更新窗体上的控件,有两个选项: 调用后台工作程序的方法,然后处理事件 调用委托在UI线程上执行更新 NB:您还可以查看相关问题以获得更详细的示例 Imports System.ComponentModel Public Class Form1 Pu
- 调用后台工作程序的方法,然后处理事件
- 调用委托在UI线程上执行更新
NB:您还可以查看相关问题以获得更详细的示例
Imports System.ComponentModel
Public Class Form1
Public Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
' This is not the UI thread.
' Trying to update controls here *will* throw an exception!!
Dim wkr = DirectCast(sender, BackgroundWorker)
For i As Integer = 0 To gv.Rows.Count - 1
' Do something lengthy
System.Threading.Thread.Sleep(100)
' Report the current progress
wkr.ReportProgress(CInt((i/gv.Rows.Count)*100))
Next
End Sub
Private Sub bgw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bgw.ProgressChanged
'everything done in this event handler is on the UI thread so it is thread safe
' Use the e.ProgressPercentage to get the progress that was reported
prg.Value = e.ProgressPercentage
End Sub
End Class
Imports System.ComponentModel
Public Class Form1
Public Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
' This is not the UI thread.
' You *must* invoke a delegate in order to update the UI.
Dim wkr = DirectCast(sender, BackgroundWorker)
For i As Integer = 0 To gv.Rows.Count - 1
' Do something lengthy
System.Threading.Thread.Sleep(100)
' Use an anonymous delegate to set the progress value
prg.Invoke(Sub() prg.Value = CInt((i/gv.Rows.Count)*100))
Next
End Sub
End Class