Det er lidt gammelt.
Men jeg har samme kode i en aeldre udgave ogsaa.
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/htroot913.zip"), HttpWebRequest)
Dim resp As HttpWebResponse = CType(req.GetResponse, HttpWebResponse)
Dim tot As Integer = CType(resp.ContentLength, Integer)
If bar.InvokeRequired Then
bar.Invoke(New ResetHandler(AddressOf ResetBar), New Object() {tot})
Else
ResetBar(tot)
End If
Dim f1 As Stream = resp.GetResponseStream
Dim f2 As Stream = New FileStream("C:\htroot913.zip", FileMode.CreateNew, FileAccess.Write)
Dim sofar As Integer = 0
Dim b(1000) As Byte
Dim n As Integer
n = f1.Read(b, 0, b.Length)
While n > 0
f2.Write(b, 0, n)
sofar += n
If bar.InvokeRequired Then
bar.Invoke(new UpdateHandler(AddressOf UpdateBar), New Object() {sofar})
Else
UpdateBar(sofar)
End If
n = f1.Read(b, 0, b.Length)
End While
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