How to show the progress of the file being downloaded?

  • Thread starter Thread starter Martin Ho
  • Start date Start date
M

Martin Ho

My application should check for new updates when user chooses this
option in the menu.
It should go online and check the flag and compare with current flag
in the programs directory. If version is newer it should start
download of the update file.
This wouldn't be a problem, but how can I show the progress of the
upgrade file which is being downloaded?

I need to show this in percents.

File might have different size from version to version and I am little
bit confused on how to do this.

Also could someone show me how to start the download?

Could someone please give me some guidelines or post some code if
possible.

Every help very much appreciated.
Thank you.

Martin
 
* (e-mail address removed)-spam.invalid (Martin Ho) scripsit:
My application should check for new updates when user chooses this
option in the menu.
It should go online and check the flag and compare with current flag
in the programs directory. If version is newer it should start
download of the update file.
This wouldn't be a problem, but how can I show the progress of the
upgrade file which is being downloaded?

I need to show this in percents.

Sample in C#:

<http://www.codeproject.com/csharp/WebDownload.asp?print=true>
 
that is great... I am looking at it right now...
In mean time if some of you have a code in vb.net, please save me a
job of re-writing it from c and post it here...
thanks..
Martin
 
I have translated the whole c# code for you to vb.net, (see bottom for
the problem):

We need someone to help us with it.

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports RJH.Utils.WebDownload


Public Class WebDownloadForm
Inherits System.Windows.Forms.Form
Private downloadUrlTextBox As System.Windows.Forms.TextBox
Private urlLabel As System.Windows.Forms.Label
Private outputGroupBox As System.Windows.Forms.GroupBox
Private downloadBtn As System.Windows.Forms.Button
Private downloadProgressLbl As System.Windows.Forms.Label
Private bytesDownloadedLbl As System.Windows.Forms.Label
Private totalBytesLbl As System.Windows.Forms.Label
Private bytesDownloadedTextBox As
System.Windows.Forms.TextBox
Private totalBytesTextBox As System.Windows.Forms.TextBox
Private progressBar As System.Windows.Forms.ProgressBar
Private components As System.ComponentModel.Container =
Nothing

Public Sub New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing
As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Private Sub InitializeComponent()
Me.downloadUrlTextBox = New System.Windows.Forms.TextBox
Me.urlLabel = New System.Windows.Forms.Label
Me.outputGroupBox = New System.Windows.Forms.GroupBox
Me.downloadBtn = New System.Windows.Forms.Button
Me.progressBar = New System.Windows.Forms.ProgressBar
Me.downloadProgressLbl = New System.Windows.Forms.Label
Me.bytesDownloadedLbl = New System.Windows.Forms.Label
Me.totalBytesLbl = New System.Windows.Forms.Label
Me.bytesDownloadedTextBox = New
System.Windows.Forms.TextBox
Me.totalBytesTextBox = New System.Windows.Forms.TextBox
Me.outputGroupBox.SuspendLayout()
Me.SuspendLayout()
Me.downloadUrlTextBox.Location = New
System.Drawing.Point(128, 16)
Me.downloadUrlTextBox.Name =
"downloadUrlTextBox"
Me.downloadUrlTextBox.Size = New
System.Drawing.Size(280, 20)
Me.downloadUrlTextBox.TabIndex = 0
Me.downloadUrlTextBox.Text = ""
Me.urlLabel.Location = New System.Drawing.Point(16,
20)
Me.urlLabel.Name = "urlLabel"
Me.urlLabel.Size = New System.Drawing.Size(100,
16)
Me.urlLabel.TabIndex = 1
Me.urlLabel.Text = "File To Download :"
Me.outputGroupBox.Controls.AddRange(New
System.Windows.Forms.Control() {Me.totalBytesTextBox,
Me.bytesDownloadedTextBox, Me.bytesDownloadedLbl,
Me.downloadProgressLbl, Me.progressBar, Me.totalBytesLbl})
Me.outputGroupBox.Enabled = False
Me.outputGroupBox.Location = New
System.Drawing.Point(8, 48)
Me.outputGroupBox.Name = "outputGroupBox"
Me.outputGroupBox.Size = New System.Drawing.Size(504,
120)
Me.outputGroupBox.TabIndex = 2
Me.outputGroupBox.TabStop = False
Me.outputGroupBox.Text = "Output and Callbacks"
Me.downloadBtn.Location = New
System.Drawing.Point(416, 16)
Me.downloadBtn.Name = "downloadBtn"
Me.downloadBtn.Size = New System.Drawing.Size(88,
23)
Me.downloadBtn.TabIndex = 3
Me.downloadBtn.Text = "Download"
AddHandler Me.downloadBtn.Click, AddressOf
Me.downloadBtn_Click
Me.progressBar.Location = New
System.Drawing.Point(120, 88)
Me.progressBar.Name = "progressBar"
Me.progressBar.Size = New System.Drawing.Size(376,
23)
Me.progressBar.TabIndex = 0
Me.downloadProgressLbl.Location = New
System.Drawing.Point(16, 88)
Me.downloadProgressLbl.Name =
"downloadProgressLbl"
Me.downloadProgressLbl.Size = New
System.Drawing.Size(104, 23)
Me.downloadProgressLbl.TabIndex = 1
Me.downloadProgressLbl.Text = "Download
Progress"
Me.bytesDownloadedLbl.Location = New
System.Drawing.Point(16, 28)
Me.bytesDownloadedLbl.Name =
"bytesDownloadedLbl"
Me.bytesDownloadedLbl.TabIndex = 2
Me.bytesDownloadedLbl.Text = "Bytes Downloaded"
Me.totalBytesLbl.Location = New
System.Drawing.Point(16, 60)
Me.totalBytesLbl.Name = "totalBytesLbl"
Me.totalBytesLbl.TabIndex = 2
Me.totalBytesLbl.Text = "Total Bytes"
Me.bytesDownloadedTextBox.Location = New
System.Drawing.Point(120, 24)
Me.bytesDownloadedTextBox.Name =
"bytesDownloadedTextBox"
Me.bytesDownloadedTextBox.ReadOnly = True
Me.bytesDownloadedTextBox.Size = New
System.Drawing.Size(168, 20)
Me.bytesDownloadedTextBox.TabIndex = 3
Me.bytesDownloadedTextBox.Text = ""
Me.bytesDownloadedTextBox.TextAlign =
System.Windows.Forms.HorizontalAlignment.Center
Me.totalBytesTextBox.Location = New
System.Drawing.Point(120, 56)
Me.totalBytesTextBox.Name = "totalBytesTextBox"
Me.totalBytesTextBox.ReadOnly = True
Me.totalBytesTextBox.Size = New
System.Drawing.Size(168, 20)
Me.totalBytesTextBox.TabIndex = 4
Me.totalBytesTextBox.Text = ""
Me.totalBytesTextBox.TextAlign =
System.Windows.Forms.HorizontalAlignment.Center
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(520, 175)
Me.Controls.AddRange(New
System.Windows.Forms.Control() {Me.downloadBtn,
Me.outputGroupBox, Me.urlLabel, Me.downloadUrlTextBox})
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Name = "WebDownloadForm"
Me.Text = "WebDownload Tester"
Me.outputGroupBox.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub

<STAThread()> _
Shared Sub Main()
Application.Run(New WebDownloadForm)
End Sub

Private Sub DownloadProgressCallback(ByVal bytesSoFar As
Integer, ByVal totalBytes As Integer)
bytesDownloadedTextBox.Text =
bytesSoFar.ToString("#,##0")
If Not (totalBytes = -1) Then
progressBar.Minimum = 0
progressBar.Maximum = totalBytes
progressBar.Value = bytesSoFar
totalBytesTextBox.Text =
totalBytes.ToString("#,##0")
Else
progressBar.Visible = False
totalBytesTextBox.Text = "Total File Size Not
Known"
End If
End Sub

Private Sub DownloadCompleteCallback(ByVal dataDownloaded
As Byte())
If Not progressBar.Visible Then
progressBar.Visible = True
progressBar.Minimum = 0
progressBar.Value = progressBar.Maximum = 1
totalBytesTextBox.Text = bytesDownloadedTextBox.Text
End If
MessageBox.Show("Download complete...",
"Download Info")
End Sub

Private Sub downloadBtn_Click(ByVal sender As Object,
ByVal e As System.EventArgs)
If Not (Me.downloadUrlTextBox.Text = "")
Then
Me.outputGroupBox.Enabled = True
Me.bytesDownloadedTextBox.Text = ""
Me.totalBytesTextBox.Text = ""
Me.progressBar.Minimum = 0
Me.progressBar.Maximum = 0
Me.progressBar.Value = 0
Dim dl As DownloadThread = New DownloadThread
dl.DownloadUrl = Me.downloadUrlTextBox.Text
AddHandler dl.CompleteCallback, AddressOf
DownloadCompleteCallback
AddHandler dl.ProgressCallback, AddressOf
DownloadProgressCallback
Dim t As System.Threading.Thread = New
System.Threading.Thread(New
System.Threading.ThreadStart(dl.Download))
t.Start()
End If
End Sub
End Class


but I can't figure out why this line is giving me an error:

[code:1:e0e46f5ac2] Dim t As System.Threading.Thread = New
System.Threading.Thread(New
System.Threading.ThreadStart(dl.Download))[/code:1:e0e46f5ac2]


Could someone please look at it?




http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
 
Ok, it workes now...

just download that example from c#.
Then use webdownload.dll and add it to your vb file refferences...
copy all forms boxes from c# to vb.net

and copy this code

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports RJH.Utils.WebDownload


Public Class WebDownloadForm
Inherits System.Windows.Forms.Form
Private downloadUrlTextBox As System.Windows.Forms.TextBox
Private urlLabel As System.Windows.Forms.Label
Private outputGroupBox As System.Windows.Forms.GroupBox
Private downloadBtn As System.Windows.Forms.Button
Private downloadProgressLbl As System.Windows.Forms.Label
Private bytesDownloadedLbl As System.Windows.Forms.Label
Private totalBytesLbl As System.Windows.Forms.Label
Private bytesDownloadedTextBox As
System.Windows.Forms.TextBox
Private totalBytesTextBox As System.Windows.Forms.TextBox
Private progressBar As System.Windows.Forms.ProgressBar
Private components As System.ComponentModel.Container =
Nothing

Public Sub New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing
As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Private Sub InitializeComponent()
Me.downloadUrlTextBox = New System.Windows.Forms.TextBox
Me.urlLabel = New System.Windows.Forms.Label
Me.outputGroupBox = New System.Windows.Forms.GroupBox
Me.downloadBtn = New System.Windows.Forms.Button
Me.progressBar = New System.Windows.Forms.ProgressBar
Me.downloadProgressLbl = New System.Windows.Forms.Label
Me.bytesDownloadedLbl = New System.Windows.Forms.Label
Me.totalBytesLbl = New System.Windows.Forms.Label
Me.bytesDownloadedTextBox = New
System.Windows.Forms.TextBox
Me.totalBytesTextBox = New System.Windows.Forms.TextBox
Me.outputGroupBox.SuspendLayout()
Me.SuspendLayout()
Me.downloadUrlTextBox.Location = New
System.Drawing.Point(128, 16)
Me.downloadUrlTextBox.Name =
"downloadUrlTextBox"
Me.downloadUrlTextBox.Size = New
System.Drawing.Size(280, 20)
Me.downloadUrlTextBox.TabIndex = 0
Me.downloadUrlTextBox.Text = ""
Me.urlLabel.Location = New System.Drawing.Point(16,
20)
Me.urlLabel.Name = "urlLabel"
Me.urlLabel.Size = New System.Drawing.Size(100,
16)
Me.urlLabel.TabIndex = 1
Me.urlLabel.Text = "File To Download :"
Me.outputGroupBox.Controls.AddRange(New
System.Windows.Forms.Control() {Me.totalBytesTextBox,
Me.bytesDownloadedTextBox, Me.bytesDownloadedLbl,
Me.downloadProgressLbl, Me.progressBar, Me.totalBytesLbl})
Me.outputGroupBox.Enabled = False
Me.outputGroupBox.Location = New
System.Drawing.Point(8, 48)
Me.outputGroupBox.Name = "outputGroupBox"
Me.outputGroupBox.Size = New System.Drawing.Size(504,
120)
Me.outputGroupBox.TabIndex = 2
Me.outputGroupBox.TabStop = False
Me.outputGroupBox.Text = "Output and Callbacks"
Me.downloadBtn.Location = New
System.Drawing.Point(416, 16)
Me.downloadBtn.Name = "downloadBtn"
Me.downloadBtn.Size = New System.Drawing.Size(88,
23)
Me.downloadBtn.TabIndex = 3
Me.downloadBtn.Text = "Download"
AddHandler Me.downloadBtn.Click, AddressOf
Me.downloadBtn_Click
Me.progressBar.Location = New
System.Drawing.Point(120, 88)
Me.progressBar.Name = "progressBar"
Me.progressBar.Size = New System.Drawing.Size(376,
23)
Me.progressBar.TabIndex = 0
Me.downloadProgressLbl.Location = New
System.Drawing.Point(16, 88)
Me.downloadProgressLbl.Name =
"downloadProgressLbl"
Me.downloadProgressLbl.Size = New
System.Drawing.Size(104, 23)
Me.downloadProgressLbl.TabIndex = 1
Me.downloadProgressLbl.Text = "Download
Progress"
Me.bytesDownloadedLbl.Location = New
System.Drawing.Point(16, 28)
Me.bytesDownloadedLbl.Name =
"bytesDownloadedLbl"
Me.bytesDownloadedLbl.TabIndex = 2
Me.bytesDownloadedLbl.Text = "Bytes Downloaded"
Me.totalBytesLbl.Location = New
System.Drawing.Point(16, 60)
Me.totalBytesLbl.Name = "totalBytesLbl"
Me.totalBytesLbl.TabIndex = 2
Me.totalBytesLbl.Text = "Total Bytes"
Me.bytesDownloadedTextBox.Location = New
System.Drawing.Point(120, 24)
Me.bytesDownloadedTextBox.Name =
"bytesDownloadedTextBox"
Me.bytesDownloadedTextBox.ReadOnly = True
Me.bytesDownloadedTextBox.Size = New
System.Drawing.Size(168, 20)
Me.bytesDownloadedTextBox.TabIndex = 3
Me.bytesDownloadedTextBox.Text = ""
Me.bytesDownloadedTextBox.TextAlign =
System.Windows.Forms.HorizontalAlignment.Center
Me.totalBytesTextBox.Location = New
System.Drawing.Point(120, 56)
Me.totalBytesTextBox.Name = "totalBytesTextBox"
Me.totalBytesTextBox.ReadOnly = True
Me.totalBytesTextBox.Size = New
System.Drawing.Size(168, 20)
Me.totalBytesTextBox.TabIndex = 4
Me.totalBytesTextBox.Text = ""
Me.totalBytesTextBox.TextAlign =
System.Windows.Forms.HorizontalAlignment.Center
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(520, 175)
Me.Controls.AddRange(New
System.Windows.Forms.Control() {Me.downloadBtn,
Me.outputGroupBox, Me.urlLabel, Me.downloadUrlTextBox})
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Name = "WebDownloadForm"
Me.Text = "WebDownload Tester"
Me.outputGroupBox.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub

<STAThread()> _
Shared Sub Main()
Application.Run(New WebDownloadForm)
End Sub

Private Sub DownloadProgressCallback(ByVal bytesSoFar As
Integer, ByVal totalBytes As Integer)
bytesDownloadedTextBox.Text =
bytesSoFar.ToString("#,##0")
If Not (totalBytes = -1) Then
progressBar.Minimum = 0
progressBar.Maximum = totalBytes
progressBar.Value = bytesSoFar
totalBytesTextBox.Text =
totalBytes.ToString("#,##0")
Else
progressBar.Visible = False
totalBytesTextBox.Text = "Total File Size Not
Known"
End If
End Sub

Private Sub DownloadCompleteCallback(ByVal dataDownloaded
As Byte())
If Not progressBar.Visible Then
progressBar.Visible = True
progressBar.Minimum = 0
progressBar.Value = progressBar.Maximum = 1
totalBytesTextBox.Text = bytesDownloadedTextBox.Text
End If
MessageBox.Show("Download complete...",
"Download Info")
End Sub

Private Sub downloadBtn_Click(ByVal sender As Object,
ByVal e As System.EventArgs)
If Not (Me.downloadUrlTextBox.Text = "")
Then
Me.outputGroupBox.Enabled = True
Me.bytesDownloadedTextBox.Text = ""
Me.totalBytesTextBox.Text = ""
Me.progressBar.Minimum = 0
Me.progressBar.Maximum = 0
Me.progressBar.Value = 0
Dim dl As DownloadThread = New DownloadThread
dl.DownloadUrl = Me.downloadUrlTextBox.Text
AddHandler dl.CompleteCallback, AddressOf
DownloadCompleteCallback
AddHandler dl.ProgressCallback, AddressOf
DownloadProgressCallback
'Dim t As System.Threading.Thread = New
System.Threading.Thread(New
System.Threading.ThreadStart(dl.Download))

Dim t As System.Threading.Thread = New
System.Threading.Thread(New
System.Threading.ThreadStart(AddressOf dl.Download))

t.Start()
End If
End Sub
End Class


I have tested it and works just perfect.
Hope I could help.

vjay
 
Thank you very much. I can't say how much you helped me.
Thanks for your time as well.
I am going to test it now.

Martin
 
Hi Herfried,

This website needs a login, this was in an other newsgroup also,
As others in that newsgroup I think you need to tell that.

(I like the EU law for protecting personal information.)

Cor
 
* "Cor said:
This website needs a login, this was in an other newsgroup also,
As others in that newsgroup I think you need to tell that.

Yes -- you will need an account (free, I have one too and I don't get
spammed).

;-)
 
Hi Herfried,

I am curious if you saw that message from Joe Nuke me.

I think he does not know what I was writing, I special did not put the
English meaning with it,

:-))

Cor
 
* "Cor said:
I am curious if you saw that message from Joe Nuke me.

Mhm... For some reason I missed it.
I think he does not know what I was writing, I special did not put the
English meaning with it,

I'll have a look at it.
 
Back
Top