Du brauchst im Prinzip einen Download Manager.
Class MainWindow
Private WithEvents mDlManager As New DownloadManager
Public Sub New()
InitializeComponent()
mDlManager.Urls.Add("http://...", "c:/...")
'...
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
mDlManager.Start()
End Sub
Private Sub mDlManager_Completed(ByVal sender As Object, ByVal e As _
DownloadManagerEventArgs) Handles mDlManager.DownloadCompleted
Debug.WriteLine("Completed: " & e.Data.Key)
End Sub
End Class
Public Class DownloadManager
Public Event DownloadCompleted(ByVal sender As Object, ByVal e As _
DownloadManagerEventArgs)
Public Property Urls As Dictionary(Of String, String)
Public Property MaximumParallelDownloads As Integer
Public Sub New()
Me.Urls = New Dictionary(Of String, String)
Me.MaximumParallelDownloads = 10
End Sub
Public Sub Start()
If Me.Urls.Count > 0 Then
For i As Integer = 0 To Math.Min(Me.Urls.Count, _
Me.MaximumParallelDownloads) - 1
Me.StartDownloadFile(Me.Urls.Keys(i), Me.Urls(Me.Urls.Keys(i)))
Next
End If
End Sub
Private Sub StartDownloadFile(ByVal url As String, ByVal path As String)
If Me.Urls.ContainsKey(url) Then Me.Urls.Remove(url)
Dim wc As New Net.WebClient
AddHandler wc.DownloadFileCompleted, AddressOf Me.DownloadFile_Completed
wc.DownloadFileAsync(New Uri(url), path, New KeyValuePair(Of String, _
String)(url, path))
End Sub
Private Sub DownloadFile_Completed(ByVal sender As Object, ByVal e As _
ComponentModel.AsyncCompletedEventArgs)
Dim wc = CType(sender, Net.WebClient)
RemoveHandler wc.DownloadFileCompleted, AddressOf _
Me.DownloadFile_Completed
Dim args = CType(e.UserState, KeyValuePair(Of String, String))
RaiseEvent DownloadCompleted(Me, New DownloadManagerEventArgs(args))
If Me.Urls.Count > 0 Then
Me.StartDownloadFile(Me.Urls.Keys(0), Me.Urls(Me.Urls.Keys(0)))
End If
End Sub
End Class
Public Class DownloadManagerEventArgs
Inherits EventArgs
Private mData As KeyValuePair(Of String, String)
Public ReadOnly Property Data As KeyValuePair(Of String, String)
Get
Return mData
End Get
End Property
Public Sub New(ByVal d As KeyValuePair(Of String, String))
mData = d
End Sub
End Class (Code ist nicht getestet)
Maas
Beitrag wurde zuletzt am 28.02.11 um 16:06:18 editiert. |