01-26-2013, 09:30 AM
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
Thanks
Backgroundworker and progress
|
01-26-2013, 09:30 AM
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
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]
Sssssssssoftware developer...
01-26-2013, 04:47 PM
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]
01-27-2013, 11:40 AM
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!
02-01-2013, 12:26 PM
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?
02-08-2013, 03:41 AM
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.
Sssssssssoftware developer...
02-15-2013, 06:18 AM
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?
02-15-2013, 01:20 PM
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
Sssssssssoftware developer...
02-21-2013, 11:55 AM
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.
|
« Next Oldest | Next Newest »
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Modding the Progress Bar | zmanalpha | 4 | 18,023 |
09-26-2012, 01:25 PM Last Post: zmanalpha |