VB.NETUpdate the Main Form's Controls
Listing 5. Try updating the ProgressBar control on the main thread from a notification on the worker thread. You do so by declaring the UpdateProgressDelegate delegate to fire the UpdateProgress method. Check the output window during the code's execution to monitor which thread is executing the lines of code in the methods. ![]() ' Create a delegate for the updates ' to the progress bar, same concept ' as the TreeView Public Delegate Sub _ UpdateProgressDelegate _ (ByVal Total As Integer, _ ByVal Count As Integer) ' This event handles the raising ' from the other thread which ' is loading the TreeNodes Private Sub OnUpdateProgress _ (ByVal Count As Integer, _ ByVal Total As Integer) Dim up As New _ UpdateProgressDelegate _ (AddressOf UpdateProgress) Dim args As Object() = {Count, Total} If Me.ProgressBar1.InvokeRequired _ Then Me.ProgressBar1. _ BeginInvoke(up, args) Else Me.UpdateProgress(Count, Total) End If End Sub ' Update the current progress ' of the node counting Sub UpdateProgress _ (ByVal Current As Integer, _ ByVal Total As Integer) Me.ProgressBar1.Maximum = Total Me.ProgressBar1.Value = Current ' Create another visual cue, ' using the StatusBar's Text ' property. In real life, you would ' use either the ProgressBar ' or the StatusBar, not both. Dim s As New String(".", Current) StatusBar1.Text = "Loading Database " _ & Current.ToString & s End Sub |