25/08/09 13:53
manvb.net
Public Class Form1 Dim TDownload As New Threading.Thread(AddressOf TCoda) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TDownload.Start() End Sub Private Sub TCoda() 'Thread di estrazione dalla coda Dim url As String = "" While True 'Ciclo infinito If ListBox1.Items.Count > 0 Then url = ListBox1.Items(0).ToString() 'Ottengo l'url ListBox1.Items.RemoveAt(0) 'Rimuovo l'elemento dalla ListBox dei download Download(url) 'Mi raccomando, il download deve essere sincrono, non asincrono End If Threading.Thread.Sleep(500) 'Diamo un attimo di respiro al pc End While End Sub End Class
Non l'ho testato, ma dovrebbe funzionare! Comunque ricorda che la routine di download deve agire in modo sincrono, non asincrono, altrimenti partono tutti i download contemporaneamente!
aaa
25/08/09 14:01
LittleHacker
mi da qwuesto tipo di errore: Operazione cross-thread non valida: è stato eseguito l'accesso al controllo 'lstVideo' da un thread diverso da quello da cui è stata eseguita la creazione. Perchè??
aaa
25/08/09 14:04
manvb.net
Si scusa, mia dimenticanza!
Modifica il form load così:
Modifica il form load così:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Control.CheckForIllegalCrossThreadCalls = False TDownload.Start() End Sub
aaa
25/08/09 14:15
manvb.net
Postato originariamente da LittleHacker:
Non vuole andare il download... ho inserito tutto ma non parte...
Non vuole andare il download... ho inserito tutto ma non parte...
In che senso non parte, puoi postare un po' di codice?
aaa
25/08/09 14:18
LittleHacker
Io inserisco l'url ma non parte non scarica neanche un file, ho provato anche a utilizzare la funzione arresta per vedere se usciva il messaggio di arresto dei download ma neanche quello...Ecco qui il codice:
é un pò lunghino...
Imports System.Net Imports System.IO Public Class frmDownload Public WithEvents TCP As New WebClient Dim total As Integer = 0 Dim downloades As Integer = 0 Dim TDownload As New Threading.Thread(AddressOf TCoda) Private Function Convert(ByVal url As String) url = url.Replace("www.youtube.com", "youtube.com") If url.IndexOf("http://youtube.com/v/") >= 0 Then url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=") End If If url.IndexOf("http://youtube.com/watch?v=") < 0 Then url = "" End If Return (url) End Function Private Function GetContent(ByVal url As String) As String Dim buffer As String Try Dim outputBuffer As String = "where=46038" Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) req.Method = "POST" req.ContentLength = outputBuffer.Length req.ContentType = "application/x-www-form-urlencoded" Dim swOut As New StreamWriter(req.GetRequestStream()) swOut.Write(outputBuffer) swOut.Close() Dim resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse) Dim sr As New StreamReader(resp.GetResponseStream()) buffer = sr.ReadToEnd() sr.Close() Catch exp As Exception buffer = "Errore: " & exp.Message.ToString() End Try Return (buffer) End Function Private Sub Download_Video(ByVal url As String) Dim buffer As String = GetContent(url) If buffer.IndexOf("Errore:") < 0 Then Dim start As Integer = 0, [end] As Integer = 0 Dim startTag As String = "/watch_fullscreen?" Dim endTag As String = ";" start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase) [end] = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase) Dim str As String = buffer.Substring(start + startTag.Length, [end] - (start + startTag.Length)) Dim vid As String = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id")) Dim l As String = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l")) Dim t As String = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t")) Dim title As String = str.Substring(str.IndexOf("&title=") + 7) title = title.Substring(0, title.Length - 1) lblvideo.Text = title TCP.DownloadFileAsync(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv")) End If End Sub Private Sub DownloadComplete(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs) Handles TCP.DownloadFileCompleted If e.Cancelled = True Then MsgBox("I/Il download sono/è stati/o annullati/o", MsgBoxStyle.Exclamation) Else MsgBox("I/Il download sono/è stati/o completati/o", MsgBoxStyle.Information) End If End Sub Private Sub PrgChngd(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles TCP.DownloadProgressChanged pbdownloadvideo.Value = e.ProgressPercentage() pbtotaldownload.Value = (((total - downloades) / total) * 100) End Sub Private Sub frmDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TDownload.Start() Control.CheckForIllegalCrossThreadCalls = False total = lstVideo.Items.Count End Sub Private Sub TCoda() 'Thread di estrazione dalla coda Dim url As String = "" While True 'Ciclo infinito If lstVideo.Items.Count > 0 Then url = lstVideo.Items(0).ToString() 'Ottengo l'url lstVideo.Items.RemoveAt(0) 'Rimuovo l'elemento dalla ListBox dei download downloades = total - lstVideo.Items.Count Download_Video(url) 'Mi raccomando, il download deve essere sincrono, non asincrono End If Threading.Thread.Sleep(500) 'Diamo un attimo di respiro al pc End While End Sub Private Sub cmdexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdexit.Click TCP.CancelAsync() Me.Close() End Sub
é un pò lunghino...
aaa
25/08/09 14:25
manvb.net
Non ho letto tutto ma credo che il problema sia quì:
TCP.DownloadFileAsync(New Uri("youtube.com/…; & vid & l & t), ("C:\video.flv")
Il file lo devi scaricare in maniera sincrona, non asincrona, e cioè:
TCP.DownloadFile(New Uri("youtube.com/…; & vid & l & t), ("C:\video.flv")
TCP.DownloadFileAsync(New Uri("youtube.com/…; & vid & l & t), ("C:\video.flv")
Il file lo devi scaricare in maniera sincrona, non asincrona, e cioè:
TCP.DownloadFile(New Uri("youtube.com/…; & vid & l & t), ("C:\video.flv")
Ultima modifica effettuata da manvb.net 25/08/09 14:26
aaa