Salve a tutti, come da titolo, il metodo DownloadFileAsync della classe Webclient, blocca il thread chiamante, ma solo la prima volta! Dopo averci battuto la testa per giorni, sono qui a chiedervi consiglio. Di seguito riporto la classe, unica, che utilizzo:
Grazie per l'attenzione.codice:Public Class Main Dim Link As String Dim Element As HtmlElement Dim ElementCollection As HtmlElementCollection Dim HTMLDownloadLinks As New ArrayList Public WithEvents Client As New Net.WebClient Dim CurrentLink As Uri Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click Me.Cursor = Cursors.WaitCursor Link = txtLink.Text If Not CheckLink(Link) Then MsgBox("Link invalid or server momentarily unreachable.", MsgBoxStyle.Critical, "Error") Me.Cursor = Cursors.Default Exit Sub Else wbBrowser.Navigate(New Uri(Link)) End If End Sub Private Function CheckLink(ByVal xLink As String) If Not Link.Contains("www") And Not Link.Contains("http") Then Link = "http://www." & Link ElseIf Link.Contains("http") And Not Link.Contains("www") Then Link = Link.Replace("http://", "") Link = "http://www." & Link End If If Not Link.Contains("http") Then Link = "http://" & Link End If Dim eLink() As String = Link.Replace("http://", "").Split("/") Try If My.Computer.Network.Ping(eLink(0)) Then Return True Catch ex As Exception Return False End Try Return False End Function Private Sub SetReady() Me.Cursor = Cursors.Default Me.Size = New Size(New Point(476, 487)) lblStatus.Text = "loaded" gbLink.Enabled = False lblStatus.ForeColor = Color.Green pnlStatus.BackColor = Color.YellowGreen End Sub Private Sub wbBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wbBrowser.DocumentCompleted SetReady() End Sub Private Sub btnGetID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetID.Click If IsNothing(wbBrowser.Document.GetElementById(txtID.Text)) Then MsgBox("No elements found.", MsgBoxStyle.Critical, "Error") Else Element = wbBrowser.Document.GetElementById(txtID.Text) HTMLDownloadLinks.Add(Element) listElement.Items.Add(Element.OuterHtml) End If End Sub Private Sub btnGetByTag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetByTag.Click If wbBrowser.Document.GetElementsByTagName(txtName.Text).Count <= 0 Then MsgBox("No elements found.", MsgBoxStyle.Critical, "Error") Else ElementCollection = wbBrowser.Document.GetElementsByTagName(txtName.Text) For Each xElement As HtmlElement In ElementCollection HTMLDownloadLinks.Add(xElement) listElement.Items.Add(xElement.OuterHtml) Next End If End Sub Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click listElement.Items.Clear() txtAttr.Text = "" txtAttrValue.Text = "" txtID.Text = "" txtName.Text = "" Element = Nothing ElementCollection = Nothing End Sub Private Sub btnSelAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelAll.Click Dim tmpCount As Int32 = listElement.Items.Count For i As Int32 = 0 To tmpCount - 1 listElement.SetSelected(i, True) Next End Sub Public Shared vCount As Int32 = 0 Public Shared Path As String Public Shared Links As New ArrayList Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click Dim Count As Int32 For Each xElement As HtmlElement In HTMLDownloadLinks If xElement.OuterHtml.Contains("<img") And listElement.SelectedItems.IndexOf(HTMLDownloadLinks(Count).OuterHTML) >= 0 Then Try Links.Add(New Uri(xElement.GetAttribute("src"))) vCount += 1 Catch ex As Exception End Try End If If xElement.OuterHtml.Contains("<a") And listElement.SelectedItems.IndexOf(HTMLDownloadLinks(Count).OuterHTML) >= 0 Then Try Links.Add(New Uri(xElement.GetAttribute("href"))) vCount += 1 Catch ex As Exception End Try End If Count += 1 Next If vCount > 0 Then SetAsyncDownloading() saveDialog.ShowDialog() Path = saveDialog.SelectedPath If Not Path = "" Then Downloadx() Else SetAsyncDownloading(True) Exit Sub End If Else MsgBox("Nothing downloadable found.", MsgBoxStyle.Critical, "Error") Exit Sub End If End Sub Private Sub btnByAttribute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnByAttribute.Click If Not txtAttr.Text = "" Or Not txtAttrValue.Text = "" Then If wbBrowser.Document.All.Count <= 0 Then MsgBox("No elements found.", MsgBoxStyle.Critical, "Error") Else ElementCollection = wbBrowser.Document.All For Each xElement As HtmlElement In ElementCollection If xElement.GetAttribute(txtAttr.Text) = txtAttrValue.Text Then HTMLDownloadLinks.Add(xElement) listElement.Items.Add(xElement.OuterHtml) End If Next End If Else MsgBox("No elements found", MsgBoxStyle.Critical, "Error") End If End Sub Private Sub SetAsyncDownloading(Optional ByVal De_set As Boolean = False) If Not De_set Then lblStatus2.Text = "Downloading... " pnlStatus.BackColor = Color.Gold Else lblStatus2.Text = "Cancelled." pnlStatus.BackColor = Color.OrangeRed End If End Sub Dim cCount As Int32 = 0 Dim xFileName As String Dim t As Threading.Thread Private Sub Downloadx() Me.Cursor = Cursors.WaitCursor btnDownload.Enabled = False btnByAttribute.Enabled = False btnGetByTag.Enabled = False btnGetID.Enabled = False Dim SplittedLinks() As String SplittedLinks = Links(cCount).ToString.Replace("http://", "").Split("/") xFileName = SplittedLinks(SplittedLinks.Length - 1) lblStatus2.Text = "Downloading... File " & (cCount + 1) & " of " & vCount Client.DownloadFileAsync(Links(cCount), Path & "\" & xFileName) cCount += 1 End Sub Private Sub Client_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles Client.DownloadFileCompleted If cCount = vCount Then pnlStatus.BackColor = Color.YellowGreen lblStatus2.Text = "Downloaded " & vCount & " files successfully." btnDownload.Enabled = True btnByAttribute.Enabled = True btnGetID.Enabled = True btnGetByTag.Enabled = True Me.Cursor = Cursors.Default Links.Clear() vCount = 0 cCount = 0 Else Downloadx() 'Eseguo il download di tutti gli elementi restanti, e selezionati, della listbox "listElement" End If End Sub End Class![]()

Rispondi quotando