1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| Public Class DownloadManager
Private _bw As BackgroundWorker
Private _progressBar As ProgressBar
Private _user As String
Private _password As String
Private _source As String
Private _cible As String
''' <summary>
''' Sets the ProgressBar object.
''' </summary>
Public WriteOnly Property ProgressBar() As ProgressBar
Set
Me._progressBar = value
End Set
End Property
Public Sub New(user As String, password As String, source As String, cible As String)
Me._user = user
Me._password = password
Me._source = source
Me._cible = cible
Me._bw = New BackgroundWorker()
Me._bw.WorkerReportsProgress = True
Me._bw.WorkerSupportsCancellation = True
Me._bw.DoWork += New DoWorkEventHandler(_bw_DoWork)
Me._bw.ProgressChanged += New ProgressChangedEventHandler(_bw_ProgressChanged)
Me._bw.RunWorkerCompleted += New RunWorkerCompletedEventHandler(_bw_RunWorkerCompleted)
End Sub
''' <summary>
''' Starts the async operation.
''' </summary>
Public Sub StartDownloadAsync()
Me._bw.RunWorkerAsync()
End Sub
''' <summary>
''' Cancels the async operation.
''' </summary>
Public Sub CancelDownloadAsync()
If Me._bw.IsBusy Then
Me._bw.CancelAsync()
End If
End Sub
''' <summary>
''' Display a message on async operation completed.
''' </summary>
Private Sub _bw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
If e.Cancelled Then
MessageBox.Show("Operation has been cancelled.")
ElseIf e.[Error] IsNot Nothing Then
MessageBox.Show("An error occured:" + e.[Error].Message)
Else
Me._progressBar.Value = 100
MessageBox.Show("Operation successfuly completed.")
End If
End Sub
''' <summary>
''' Report async operation progress changed.
''' </summary>
Private Sub _bw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
Me._progressBar.Value = e.ProgressPercentage
End Sub
''' <summary>
''' Download the file.
''' </summary>
Private Sub _bw_DoWork(sender As Object, e As DoWorkEventArgs)
' Get the size of the file to download
Dim url = New Uri(Me._source)
Dim request = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response = DirectCast(request.GetResponse(), HttpWebResponse)
response.Close()
' Gets file size
Dim sourceFileSize = response.ContentLength
' Stores total downloaded bytes in order to allow progress reporting
Dim totalBytesDownloaded As Long = 0
' Download the file using a WebClient object
Using webClient = New WebClient()
' Open source file and read it
Using stream = webClient.OpenRead(url)
' Write downloaded bytes to hard drive
Using fileStream = New FileStream(Me._cible, FileMode.Create, FileAccess.Write, FileShare.None)
' Get file into buffer
Dim bufferSize = 0
Dim buffer = New Byte(sourceFileSize - 1) {}
While (InlineAssignHelper(bufferSize, stream.Read(buffer, 0, buffer.Length))) > 0
fileStream.Write(buffer, 0, bufferSize)
totalBytesDownloaded += bufferSize
' Compute progress in base 100
Dim index = CDbl(totalBytesDownloaded)
Dim total = CDbl(buffer.Length)
Dim progressPercentage As Double = (index / total)
Dim progress = CInt(progressPercentage * 100)
' Report progress
Me._bw.ReportProgress(progress)
End While
End Using
End Using
End Using
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class |
Partager