How to update the ProgressBar in the main form using a thread?

danyeungw

Well-known member
Joined
Aug 30, 2005
Messages
73
Programming Experience
10+
I dropped a ProgressBar on a form. In the form button_click event, it uses a thread to update the ProgressBar. Please the code below. In the Worker1 class, Console.Write("A") worked but not objForm.ProgressBarMeter1.Value = i which means the ProgressBar did not updated. Any suggestion how the ProgressBar can be updated? Thanks.

VB.NET:
Public Class FormLab11
Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
Dim objWorker1 As New Worker1
Dim objWorker2 As New Worker2
Dim t1 As Thread
Dim t2 As Thread
t1 = New Thread(AddressOf objWorker1.Meter1)
t2 = New Thread(AddressOf objWorker2.Meter2)
t1.Start()
t2.Start()
Do While t1.IsAlive
t2.Join()
Loop
End Sub
End Class
 
Public Class Worker1
Public Sub Meter1()
Dim objForm As New FormLab11
For i As Integer = 0 To 10000
objForm.ProgressBarMeter1.Value = i
Console.Write("A")
Thread.Sleep(1)
Next
End Sub
End Class
Thanks.
DanYeung
 
Last edited by a moderator:
In .Net 2 as cjard said the BackgroundWorker class is easiest to work with for asynchronous operations.

Else with regular threads you have to invoke a delegated method on the thread that created the control to make thread-safe calls on it, usually the main thread.

Best way when you have no knowledge is to read a book chapter and lots of tutorials on the topic then dig into the documentation reference when you know what to read up on specifically.
 
If .NET 2.0 is your main platform can you please change your profile to reflect that. If your question relates to a version other thna your main platform please always specify.
 
Back
Top