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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| Imports System.IO
Imports System.Threading
Public Class Downloader
Private _instances As New ArrayList()
Private _threads As New ArrayList()
Private _activeDownloadCount As Integer = 0
Private _lockObject As New Object()
Private Delegate Sub delSetStatus(ByVal Status As String, ByVal Mycolor As Color, ByVal f As FileDownloader)
Public Shared Sub InfoDownloader(Names As String, URL As String, destination As String)
Downloader._instances = New ArrayList()
Downloader._threads = New ArrayList()
Downloader._activeDownloadCount = 0
Dim download As FileDownloader = Nothing
Downloader.LvDownload.Items.Add(New ListViewItem(New String() {Names, ""}))
For Each item As ListViewItem In Downloader.LvDownload.Items
item.SubItems(1).Text = "Patienter le téléchargement...."
item.SubItems(1).ForeColor = Color.Blue
item.UseItemStyleForSubItems = False
download = New FileDownloader(URL, Path.Combine(destination, Names & Path.GetExtension(URL)))
item.Tag = download
Try
Dim tsDelegate As New ThreadStart(AddressOf download.StartDownload)
AddHandler download.DownloadStarting, New FileDownloader._DownloadStarting(AddressOf download_DownloadStarting)
AddHandler download.DownloadCompleted, New FileDownloader.DownloadCompletedEventHandler(AddressOf download_DownloadCompleted)
AddHandler download.DownloadProgressChanged, New FileDownloader.ProgressChangedEventHandler(AddressOf download_DownloadProgress)
Dim t As New Thread(tsDelegate)
t.Name = item.SubItems(0).Text
Downloader._threads.Add(t)
Downloader._instances.Add(download)
Catch ex As ObjectDisposedException
item.SubItems(1).Text = "Erreur de téléchargement !"
item.SubItems(1).ForeColor = Color.Red
End Try
Next
StartDownload()
End Sub
Private Shared Sub download_DownloadCompleted(thread As FileDownloader, e As DownloadCompletedArgs)
If e.Error IsNot Nothing Then
SetStatus("Erreur de Téléchargement...", Color.Red, thread)
Return
Else
SyncLock Downloader._lockObject
Downloader._activeDownloadCount -= 1
End SyncLock
SetStatus("Téléchargement terminé...", Color.Blue, thread)
StartDownload()
Return
End If
End Sub
Private Shared Sub download_DownloadProgress(thread As FileDownloader, e As DownloadProgressArgs)
SetStatus("Téléchargement( " & Downloader.GetFileSize(e.TotalBytesReceived) & " \ " & Downloader.GetFileSize(e.BytesReceived) & " ) - " & e.ProgressPercentage & " %", Color.Green, thread)
End Sub
Private Shared Sub download_DownloadStarting(ByVal thread As FileDownloader)
SetStatus("Le téléchargement va bientôt commencer...", Color.Green, thread)
End Sub
Private Shared Sub StartDownload()
Dim j As Integer = 0
Dim limit As Integer = CInt(Form1.NumericUpDown1.Value)
Dim iCount As Integer = 0
SyncLock Downloader._lockObject
iCount = Downloader._instances.Count
End SyncLock
If (iCount <> 0) Then
For Each thread As Thread In Downloader._threads
Dim file As FileDownloader = CType(Downloader._instances(j), FileDownloader)
If file._IsStarted = False Then
SyncLock Downloader._lockObject
thread.Start()
Downloader._activeDownloadCount += 1
End SyncLock
End If
If Downloader._activeDownloadCount = limit Then
Exit For
End If
j += 1
Next
End If
End Sub
Private Shared Sub SetStatus(ByVal Status As String, ByVal colorLVi As Color, ByVal f As FileDownloader)
If Downloader.LvDownload.InvokeRequired Then
Dim method As New delSetStatus(AddressOf SetStatus)
Downloader.Invoke(method, New Object() {Status, colorLVi, f})
Else
For Each item As ListViewItem In Downloader.LvDownload.Items
If item.Tag Is f Then
SyncLock Downloader._lockObject
item.SubItems(1).Text = Status
item.SubItems(1).ForeColor = colorLVi
End SyncLock
Exit For
End If
Next
End If
End Sub
Private Function GetFileSize(Type As Long) As String
Dim types As String() = {"Byte", "KB", "MB", "GB"}
Dim typees As Double = Type
Dim CSA As Integer = 0
While typees >= 1024 AndAlso CSA + 1 < types.Length
CSA += 1
typees = typees / 1024
End While
Return [String].Format("{0:0.##} {1}", typees, types(CSA))
End Function
Private Sub Download_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Me._threads.Count > 0 Then
If (MessageBox.Show("Voulez-vous annuler les téléchargements actifs.", "Annulé", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK) Then
Me.KillThreads()
Else
e.Cancel = True
End If
End If
End Sub
Private Sub KillThreads()
For Each thread As Thread In Me._threads
If thread.IsAlive Then
thread.Abort()
thread.Interrupt()
End If
Next
Me._threads = New ArrayList
Me._instances = New ArrayList
End Sub
End Class |
Partager