01-26-2013, 11:29 AM
There are 2 ways of doing that through delegates or through reporting its progress i'll demonstrate both ways
First and the easiest is through reporting its progress:
[code2=vbnet]Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim i As Integer = 0
For i = 0 To 100 Step 1
Threading.Thread.Sleep(50)
BackgroundWorker1.ReportProgress(i)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
ProgressBar1.Value = 0
End Sub[/code2]
The second method is through delegates it is more complicated but i prefer this one when i need to modify UI on the form
[code2=vbnet]Public Delegate Sub IncrementPregressbarCallBack(ByVal value As Integer)
Private Sub IncrementProgressbar(ByVal value As Integer)
If Me.ProgressBar1.InvokeRequired Then
Dim s As New IncrementPregressbarCallBack(AddressOf IncrementProgressbar)
Me.Invoke(s, New Object() {value})
Else
ProgressBar1.Value = value
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim i As Integer = 0
For i = 0 To 100 Step 1
Threading.Thread.Sleep(50)
IncrementProgressbar(i)
Next
End Sub[/code2]
First and the easiest is through reporting its progress:
[code2=vbnet]Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim i As Integer = 0
For i = 0 To 100 Step 1
Threading.Thread.Sleep(50)
BackgroundWorker1.ReportProgress(i)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
ProgressBar1.Value = 0
End Sub[/code2]
The second method is through delegates it is more complicated but i prefer this one when i need to modify UI on the form
[code2=vbnet]Public Delegate Sub IncrementPregressbarCallBack(ByVal value As Integer)
Private Sub IncrementProgressbar(ByVal value As Integer)
If Me.ProgressBar1.InvokeRequired Then
Dim s As New IncrementPregressbarCallBack(AddressOf IncrementProgressbar)
Me.Invoke(s, New Object() {value})
Else
ProgressBar1.Value = value
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim i As Integer = 0
For i = 0 To 100 Step 1
Threading.Thread.Sleep(50)
IncrementProgressbar(i)
Next
End Sub[/code2]
Sssssssssoftware developer...