Public Class Download
#Region "EventArgs"
Public Class DownloadProgressChangedEventArgs
Inherits EventArgs
Dim np As Decimal
Public Sub New(ByVal NewProgress As Decimal)
np = NewProgress
End Sub
Public ReadOnly Property NewProgress() As Decimal
Get
Return np
End Get
End Property
End Class
Public Class DownloadSpeedChangedEventArgs
Inherits EventArgs
Dim s As Decimal
Public Sub New(ByVal Speed As Decimal)
s = Speed
End Sub
Public ReadOnly Property Speed() As Decimal
Get
Return s
End Get
End Property
End Class
Public Class DownloadCompletedEventArgs
Inherits EventArgs
Dim ms As Decimal
Public Sub New(ByVal MediumSpeed As Decimal)
ms = MediumSpeed
End Sub
Public ReadOnly Property MediumSpeed() As Decimal
Get
Return ms
End Get
End Property
End Class
Public Class DownloadStartedEventArgs
Inherits EventArgs
Dim fs As Decimal
Public Sub New(ByVal FileSize As Decimal)
fs = FileSize
End Sub
Public ReadOnly Property FileSize() As Decimal
Get
Return fs
End Get
End Property
End Class
#End Region
#Region "Events"
Public Event DownloadStarted As EventHandler(Of Download.DownloadStartedEventArgs)
Public Event DownloadCompleted As EventHandler(Of Download.DownloadCompletedEventArgs)
Public Event DownloadPaused As EventHandler
Public Event DownloadResumed As EventHandler
Public Event DownloadAborted As EventHandler
Public Event DownloadProgressChanged As EventHandler(Of Download.DownloadProgressChangedEventArgs)
Public Event DownloadSpeedChanged As EventHandler(Of Download.DownloadSpeedChangedEventArgs)
#End Region
#Region "Attributes"
Dim numerosocket As Decimal, sec As Decimal, bn As Decimal
Dim tl As New List(Of Thread), v As New List(Of Decimal)
Dim ds As FileStream, ss As Stream
Dim WithEvents Timer1 As New Windows.Forms.Timer With {.Interval = 1000}
Dim wc As New WebClient
#End Region
#Region "Constructors"
Public Sub New(ByVal url As String, ByVal filename As String, ByVal threads As Decimal)
ss = wc.OpenRead(url)
ds = New FileStream(filename, FileMode.Create)
numerosocket = threads
sec = 0
bn = 0
tl = New List(Of Thread)
v = New List(Of Decimal)
End Sub
#End Region
#Region "Private methods"
Private Function MediumSpeed() As Decimal
Dim r As Decimal = 0
For i As Integer = 0 To v.Count - 1
r += v(i)
Next
Return r / v.Count
End Function
Private Sub Download()
While True
Try
ds.WriteByte(ss.ReadByte)
Catch ex As NotSupportedException
GoTo fine
Catch
Exit While
End Try
bn += 1
'RaiseEvent DownloadProgressChanged(Me, New DownloadProgressChangedEventArgs(bn))
End While
If tl.Count = 1 Then
Timer1.Stop()
tl.Clear()
ds.Close()
ss.Close()
sec = 0
RaiseEvent DownloadCompleted(Me, New DownloadCompletedEventArgs(MediumSpeed))
v.Clear()
Else
fine: tl.Remove(Thread.CurrentThread)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
On Error Resume Next
sec += 1
Dim speed As Decimal = bn / sec
v.Add(speed)
'RaiseEvent DownloadSpeedChanged(Me, New DownloadSpeedChangedEventArgs(speed))
End Sub
#End Region
#Region "Public methods"
Public Sub StartDownload()
RaiseEvent DownloadStarted(Me, New DownloadStartedEventArgs(wc.ResponseHeaders(HttpResponseHeader.ContentLength)))
For i As Integer = 1 To numerosocket
Dim t As New Thread(AddressOf Download)
tl.Add(t)
t.Start()
Next
Timer1.Start()
End Sub
Public Sub PauseDownload()
On Error Resume Next
For i As Integer = 0 To tl.Count - 1
tl(i).Suspend()
Next
Timer1.Stop()
RaiseEvent DownloadPaused(Me, New EventArgs)
End Sub
Public Sub ResumeDownload()
On Error Resume Next
RaiseEvent DownloadResumed(Me, New EventArgs)
For i As Integer = 0 To tl.Count - 1
tl(i).Resume()
Next
Timer1.Start()
End Sub
Public Sub AbortDownload()
On Error Resume Next
For i As Integer = 0 To tl.Count - 1
tl(i).Abort()
Next
Timer1.Stop()
tl.Clear()
ds.Close()
ss.Close()
sec = 0
v.Clear()
RaiseEvent DownloadAborted(Me, New EventArgs)
End Sub
#End Region
End Class