VB.Net ProgressChanged无效

时间:2015-02-20 08:19:41

标签: .net vb.net windows multithreading backgroundworker

不知何故,我无法在backgroundWorker_ProgressChanged函数中更改progressbar.value。我收到System.InvalidOperationException例外。

这是我的代码:

Private Sub backgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles backgroundWorker.DoWork


    ' do all this funny work!
    backgroundWorker.ReportProgress(30)
End Sub

Private Sub backgroundWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles backgroundWorker.ProgressChanged
    pb.Value = e.ProgressPercentage
End Sub

WorkerReportsProgress为True。

这个例子(它的德语很抱歉,但我希望无论如何都有帮助):

  

System.InvalidOperationException wurde nicht behandelt。
  HResult = -2146233079消息=UngültigerthreadübergreifenderVorgang:   Der Zugriff auf das Steuerelement pb erfolgte von einem anderen Thread   als dem Thread,fürdenes erstellt wurde。
  Source = System.Windows.Forms StackTrace:          bei System.Windows.Forms.Control.get_Handle()          bei System.Windows.Forms.Control.SendMessage(Int32 msg,Int32 wparam,Int32 lparam)          bei System.Windows.Forms.ProgressBar.UpdatePos()          bei System.Windows.Forms.ProgressBar.set_Value(Int32 value)          bei SKDateiTransfer.frmMain.backgroundWorker_ProgressChanged(Object   发件人,ProgressChangedEventArgs e)在C:\ Users \ hakst \ Documents \ Visual中   Studio 2013 \ Projects \ SKDateiTransfer \ SKDateiTransfer \ frmMain.vb:Zeile   198。          bei System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs   E)          bei System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)          bei System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object   州)          bei System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)          bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)          bei System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()          bei System.Threading.ThreadPoolWorkQueue.Dispatch()          bei System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  的InnerException:

0 个答案:

没有答案