Backgroundworker and progress - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8) +----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9) +----- Thread: Backgroundworker and progress (/showthread.php?tid=740) |
Backgroundworker and progress - brco900033 - 01-26-2013 Hello everyone. I want a BackgroundWorker reports its progress and send that to a ProgressBar. I did alot of research on the internet but I can't get it working. Thanks Re: Backgroundworker and progress - Snake_eyes - 01-26-2013 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] Re: Backgroundworker and progress - brandonio21 - 01-26-2013 Thanks a lot for your reply, Snake_eyes. Your method would definitely work. However, don't you have to also call Refresh on the progress bar? As in, [code2=vbnet]'progress bar value change ProgressBar1.Refresh()[/code2] Re: Backgroundworker and progress - Snake_eyes - 01-27-2013 No you don't the progress bar will work fine. Re: Backgroundworker and progress - brandonio21 - 01-27-2013 Snake_eyes Wrote:No you don't the progress bar will work fine.Awesome! I always thought you had to call refresh. Guess I was wrong! Re: Backgroundworker and progress - brco900033 - 02-01-2013 Thanks for the code, I've tested it and it worked but isn't it like a 'pre-defined' progress? I want to download a file and report the progress but it only wants to download the file after the progress reached 100. Maybe I put the download code in the wrong place? Re: Backgroundworker and progress - Snake_eyes - 02-08-2013 The exaple i gave you is a general one on how to simply increment the progress bar, you have to modify the snippet for your specific use. Quote:Maybe I put the download code in the wrong place?If it starts the download after the progress bar is at 100% you did put the code in the wrong place but until you don't post your download code i cannot help you. Re: Backgroundworker and progress - brco900033 - 02-15-2013 Sorry for the late reply but I was a little busy the last few weeks. My download code is: Code: Dim downloader As New System.Net.WebClient() Does the progress reporting of a BackgroundWorker work with all codes or are there restrictions? Re: Backgroundworker and progress - Snake_eyes - 02-15-2013 Well in that case you dont need a background worker to display the download progress because WebClient object has it's own set of event handlers just declare the web client under the main class of your project Example: [code2=vbnet]Public Class Form1 Dim WithEvents WC As New WebClient[/code2] Then start the download : [code2=vbnet]Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click WC.DownloadFileAsync("Address", "FileName") End Sub[/code2] Increment the progressbar acording to the download percentage: [code2=vbnet]Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged ProgressBar1.Value = e.ProgressPercentage End Sub[/code2] This is the most simple way of downloadng a file and showing progress in a progressbar. You can also use a background worker but that will only complicate things NOTE : Leave the progressbar at it's default value of 100 because e.ProgressPercentage should be a integer no larger than 100 Re: Backgroundworker and progress - brco900033 - 02-21-2013 Thanks, needed this alot! A downloader without a progressbar isn't good at all. I didn't know it is possible to report the progress without a backgroundworker. |