I wrote a thread before, but my login session timed out and when I went to post it, I lost everything I wrote, so I am going to keep this one short.
I am creating a program to download files from websites.
My UI freezes when downloading large files and so after days of research looking into other ways of updating UI with download progress information, I found out that the backgroundworker would be most suitable.
I've tried using the webclient downloadfileasync method but because I am using a loop event the download continues and the code doesn't pause for the download to finish. So I couldn't use that.
Then I decided to work with background worker and that did the job great, I could download the file and UI didn't freeze but now I can't show my download progress information because I can't call the progress update method.
This is my ProgressUpdate sub:
I got more code for progress bar and such but I don't think that is necessary.
This is my Initialize Component.
And as you can see I also tried to use the backgroundworker class progress changed event, but that didn't work for me either.
This is my do work and completed subs.
Thanks.
I am creating a program to download files from websites.
My UI freezes when downloading large files and so after days of research looking into other ways of updating UI with download progress information, I found out that the backgroundworker would be most suitable.
I've tried using the webclient downloadfileasync method but because I am using a loop event the download continues and the code doesn't pause for the download to finish. So I couldn't use that.
Then I decided to work with background worker and that did the job great, I could download the file and UI didn't freeze but now I can't show my download progress information because I can't call the progress update method.
This is my ProgressUpdate sub:
Code:
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
Dim percentage As Double = bytesIn / totalBytes * 100
ProgressBarCurrent.Value = Int32.Parse(Math.Truncate(percentage).ToString())
Dim BytesDownloaded As String = (e.BytesReceived / (DirectCast(e.UserState, Stopwatch).ElapsedMilliseconds / 1000.0#)).ToString("#")
If BytesDownloaded < 1024 Then
Dim Bs As String = Convert.ToInt32(BytesDownloaded)
Label4.Text = (Bs & " B/s")
ElseIf BytesDownloaded < 1048576 Then
Dim KBs As String = Math.Round(BytesDownloaded / 1024, 2)
Label4.Text = (KBs & " KB/s")
ElseIf BytesDownloaded < 1073741824 Then
Dim MBs As String = Math.Round(BytesDownloaded / 1048576, 2)
Label4.Text = (MBs & " MB/s")
ElseIf BytesDownloaded < 1099511627776 Then
Dim GBs As String = Math.Round(BytesDownloaded / 1073741824, 2)
Label4.Text = (GBs & " GB/s")
Else
Label4.Text = ("Estimating...")
End IfThis is my Initialize Component.
Code:
Public Sub New()
InitializeComponent()
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler wc.DownloadProgressChanged, AddressOf bw_ProgressChanged
'AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
End SubThis is my do work and completed subs.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not bw.IsBusy = True Then
bw.RunWorkerAsync()
End If
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'I got some code here. If you want to see it just post.
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
LabelStatus.Text = "Download Complete"
Label4.Text = ("-")
Button1.Enabled = True
Downloading = False
End Sub