Hallo!
Meiner Ansicht nach ist es in dem Fall nicht die GUI,
sondern die Verwendung der ToString-Methode,
die die Aktualisierung blockiert.
Ich würde empfehlen, für den Worker-Report eine geeignete Klasse zu
erstellen, und als e.Userstate-Parameter eine Instanz dieser Klasse,
Properties gefüllt mit den aktuellen Report-Daten, zu übergeben.
Option Strict On
Public Class WorkerState
Public Property ProgressPIValue As Double
Public Property ProgressPILoop As Single
Public Property InfoText As String
End Class
Public Class clsThreadTest
Private WithEvents WORKER As New System.ComponentModel.BackgroundWorker
Public Event NewINfo(ByVal InfoNr As Integer, ByVal ws As WorkerState)
Public Sub StartScan()
Me.WORKER.WorkerReportsProgress = True
Me.WORKER.WorkerSupportsCancellation = True
Me.WORKER.RunWorkerAsync()
End Sub
Public Sub CancelScan()
If Me.WORKER.IsBusy Then
Me.WORKER.CancelAsync()
End If
End Sub
Private Sub worker_DoWork(ByVal sender As Object, ByVal e As _
System.ComponentModel.DoWorkEventArgs) Handles WORKER.DoWork
Dim I As Integer
Dim j As Single
Dim ws As New WorkerState
ws.ProgressPIValue = 0
ws.Infotext = CStr(Now) & " - Start"
WORKER.ReportProgress(0, ws)
For I = 1 To 100
WORKER.ReportProgress(I, ws)
Dim PI As Double = 0
For j = 1 To 400
PI = 0
For ii As Integer = 0 To 100
PI += 1 / Math.Pow(16, ii) * (4 / ((8 * ii) + 1) - 2 / ((8 _
* ii) + 4) - 1 / ((8 * ii) + 5) - 1 / ((8 * ii) + 6))
Next
ws.InfoText = ""
ws.ProgressPIValue = PI
ws.ProgressPILoop = j
WORKER.ReportProgress(I, ws)
Next
If WORKER.CancellationPending = True Then Exit For
Next
ws.Infotext = CStr(Now) & " - Ende"
WORKER.ReportProgress(100, ws)
End Sub
Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As _
System.ComponentModel.ProgressChangedEventArgs) Handles _
WORKER.ProgressChanged
RaiseEvent NewINfo(e.ProgressPercentage, CType(e.UserState, _
WorkerState))
End Sub
End Class Verarbeitung:
Private Sub Scan1_NewINfo(ByVal ProgressPercentage As Integer, _
ByVal ws As BGW_Test.WorkerState) Handles Scan1.NewINfo
lblinfo_loop1.Text = CStr(ProgressPercentage)
lblinfo_loop2.Text = CStr(ws.ProgressPILoop) & ": " & CStr( _
ws.ProgressPIValue)
If Not String.IsNullOrEmpty(ws.InfoText) Then
rtblog.AppendText(ws.InfoText & vbCrLf)
End If
End Sub
Beitrag wurde zuletzt am 27.03.12 um 11:06:23 editiert. |