Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Backgroundworker and progress
#2
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]
Sssssssssoftware developer...


Messages In This Thread
Backgroundworker and progress - by brco900033 - 01-26-2013, 09:30 AM
Re: Backgroundworker and progress - by Snake_eyes - 01-26-2013, 11:29 AM
Re: Backgroundworker and progress - by Snake_eyes - 01-27-2013, 07:55 AM
Re: Backgroundworker and progress - by brco900033 - 02-01-2013, 12:26 PM
Re: Backgroundworker and progress - by Snake_eyes - 02-08-2013, 03:41 AM
Re: Backgroundworker and progress - by brco900033 - 02-15-2013, 06:18 AM
Re: Backgroundworker and progress - by Snake_eyes - 02-15-2013, 01:20 PM
Re: Backgroundworker and progress - by brco900033 - 02-21-2013, 11:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Modding the Progress Bar zmanalpha 4 18,006 09-26-2012, 01:25 PM
Last Post: zmanalpha

Forum Jump:


Users browsing this thread: 1 Guest(s)