P
Peter Proost
Hi group,
I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if it's
the ok way to do it? Can I improve something? What's the best practise for
updating multiple controls on my form for example 5 labels or so, just keep
passing them to the constructor?
Greetz, Peter
On my Form:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()
End Sub
'The worker thread
Imports System.Threading
'======================================================================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)
Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)
Private progy As ProgressBar
Private infoLbl As Label
Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub
Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub
Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next
End Sub
Public Sub IncreaseValue(ByVal myVal As Integer)
progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub
End Class
I have been doing some reading on threading and updating the ui from a
worker thread and I made this sample which works but I was wondering if it's
the ok way to do it? Can I improve something? What's the best practise for
updating multiple controls on my form for example 5 labels or so, just keep
passing them to the constructor?
Greetz, Peter
On my Form:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oMyUpd As New MyUpdater(ProgressBar1, lblPct)
oMyUpd.Start()
End Sub
'The worker thread
Imports System.Threading
'======================================================================
Public Class MyUpdater
Delegate Sub UpdateStarter()
Delegate Sub FeedBack(ByVal myValue As Integer)
Private oThread As Thread
Private oMyUpdate As New UpdateStarter(AddressOf StartUpdating)
Private progy As ProgressBar
Private infoLbl As Label
Public Sub New(ByVal myProg As ProgressBar, ByVal myLabel As Label)
progy = myProg
infoLbl = myLabel
End Sub
Public Sub [Start]()
oMyUpdate.BeginInvoke(Nothing, Nothing)
End Sub
Public Sub StartUpdating()
Dim giveFeedBack As New FeedBack(AddressOf IncreaseValue)
oThread = Thread.CurrentThread
For i As Integer = 0 To 15
progy.BeginInvoke(giveFeedBack, New Object() {i})
Thread.Sleep(200)
Next
End Sub
Public Sub IncreaseValue(ByVal myVal As Integer)
progy.Value = myVal
infoLbl.Text = CStr(Math.Round(myVal / progy.Maximum * 100, 2))
End Sub
End Class