Jeg tvivler paa My.Computer.Network.DownloadFile og ProgressBar spiller godt sammen.
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Net
Imports System.Windows.Forms
Imports System.Threading
Public Class MainForm
Inherits Form
Private bar As ProgressBar
Private start As Button
Private abort As Button
Private t As Thread
Public Sub New()
bar = New ProgressBar
start = New Button
abort = New Button
SuspendLayout
bar.Location = New Point(50, 50)
bar.Size = New Size(200, 50)
bar.Name = "Progress Bar"
start.Location = New Point(50, 150)
start.Size = New Size(200, 50)
start.Name = "Start Button"
start.Text = "Start"
AddHandler start.Click, AddressOf StartClick
abort.Location = New Point(50, 250)
abort.Size = New Size(200, 50)
abort.Name = "Abort Button"
abort.Text = "Abort"
AddHandler abort.Click, AddressOf AbortClick
ClientSize = New Size(300, 350)
Controls.Add(bar)
Controls.Add(start)
Controls.Add(abort)
Name = "Main Form"
Text = "Main Form"
ResumeLayout(False)
End Sub
Sub ResetBar(ByVal n As Integer)
bar.Minimum = 0
bar.Maximum = n
bar.Value = 0
End Sub
Sub UpdateBar(ByVal n As Integer)
bar.Value = n
End Sub
Delegate Sub ResetHandler(ByVal n As Integer)
Delegate Sub UpdateHandler(ByVal n As Integer)
Sub Copy()
Try
Dim req As HttpWebRequest = CType(WebRequest.Create("
http://wasd.vsm.com.au/wasd/wasd1020.zip"), HttpWebRequest)
Dim resp As HttpWebResponse = CType(req.GetResponse, HttpWebResponse)
Dim tot As Integer = CType(resp.ContentLength, Integer)
If bar.InvokeRequired Then
bar.Invoke(DirectCast(Sub() ResetBar(tot), Action))
Else
ResetBar(tot)
End If
Dim f1 As Stream = resp.GetResponseStream
Dim f2 As Stream = New FileStream("C:\work\wasd1020.zip", FileMode.Create, FileAccess.Write)
Dim sofar As Integer = 0
Dim b(1000) As Byte
Dim n As Integer
Do
n = f1.Read(b, 0, b.Length)
If n < 0 Then Exit Do
f2.Write(b, 0, n)
sofar += n
If bar.InvokeRequired Then
bar.Invoke(DirectCast(Sub() UpdateBar(sofar), Action))
Else
UpdateBar(sofar)
End If
Loop
f2.Close
f1.Close
resp.Close
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub StartClick(ByVal sender As Object, ByVal e As EventArgs)
t = New Thread(AddressOf Copy)
t.Start
End Sub
Sub AbortClick(ByVal sender As Object, ByVal e As EventArgs)
t.Abort
End Sub
<STAThread()> _
Public Shared Sub Main(ByVal args As String())
Application.Run(New MainForm)
Application.Exit
Environment.Exit(0)
End Sub
End Class