Updating GUI with backgroundworker
I was hoping I can do that with timer, but its not working, like anything else I tried & all the codes I found I simply Dont understand ( Even TUTORIALS!!! why are they so complicated? ), so let ma ask for a simple example: how I make my label change?
This is what I want to do:
Code:
For i = 0 To 1000
System.Threading.Thread.Sleep(1000)
Me.Label1.Text = i
Next
And this is the code im working on:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.BackgroundWorker1.RunWorkerAsync() ' start worker
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 To 1000
System.Threading.Thread.Sleep(1000)
Me.Label1.Text = i
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles _
BackgroundWorker1.ProgressChanged
' not really shure how to use this...
End Sub
Re: Updating GUI with backgroundworker
While it is technically not illegal to manipulate controls across threads, this is prone to some serious errors.
Use these tutorials by jmc, I found them invaluable when getting acquainted with the BGW.
Using the Background Worker Component
Accessing Controls from Worker Threads