Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Coda di download
Forum - C# / VB.NET - Coda di download - Pagina 2

Pagine: [ 1 2 3 4 ] Precedente | Prossimo
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 15:53
Martedì, 25/08/2009
Codice sorgente - presumibilmente VB.NET

  1. Public Class Form1
  2.     Dim TDownload As New Threading.Thread(AddressOf TCoda)
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         TDownload.Start()
  5.     End Sub
  6.     Private Sub TCoda() 'Thread di estrazione dalla coda
  7.         Dim url As String = ""
  8.         While True 'Ciclo infinito
  9.             If ListBox1.Items.Count > 0 Then
  10.                 url = ListBox1.Items(0).ToString() 'Ottengo l'url
  11.                 ListBox1.Items.RemoveAt(0) 'Rimuovo l'elemento dalla ListBox dei download
  12.                 Download(url) 'Mi raccomando, il download deve essere sincrono, non asincrono
  13.             End If
  14.             Threading.Thread.Sleep(500) 'Diamo un attimo di respiro al pc
  15.         End While
  16.     End Sub
  17. 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!

PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 16:01
Martedì, 25/08/2009
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è?? :om:

PM Quote
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 16:04
Martedì, 25/08/2009
Si scusa, mia dimenticanza!

Modifica il form load così:

Codice sorgente - presumibilmente VB.NET

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         Control.CheckForIllegalCrossThreadCalls = False
  3.         TDownload.Start()
  4.     End Sub


PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 16:12
Martedì, 25/08/2009
Non vuole andare il download... ho inserito tutto ma non parte...:om:

PM Quote
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 16:15
Martedì, 25/08/2009
Testo quotato

Postato originariamente da LittleHacker:

Non vuole andare il download... ho inserito tutto ma non parte...:om:



In che senso non parte, puoi postare un po' di codice?

PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 16:18
Martedì, 25/08/2009
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:

Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net
  2. Imports System.IO
  3. Public Class frmDownload
  4.     Public WithEvents TCP As New WebClient
  5.     Dim total As Integer = 0
  6.     Dim downloades As Integer = 0
  7.     Dim TDownload As New Threading.Thread(AddressOf TCoda)
  8.  
  9.     Private Function Convert(ByVal url As String)
  10.         url = url.Replace("www.youtube.com", "youtube.com")
  11.         If url.IndexOf("http://youtube.com/v/") >= 0 Then
  12.             url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=")
  13.         End If
  14.         If url.IndexOf("http://youtube.com/watch?v=") < 0 Then
  15.             url = ""
  16.         End If
  17.         Return (url)
  18.     End Function
  19.  
  20.     Private Function GetContent(ByVal url As String) As String
  21.         Dim buffer As String
  22.         Try
  23.             Dim outputBuffer As String = "where=46038"
  24.             Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
  25.             req.Method = "POST"
  26.             req.ContentLength = outputBuffer.Length
  27.             req.ContentType = "application/x-www-form-urlencoded"
  28.             Dim swOut As New StreamWriter(req.GetRequestStream())
  29.             swOut.Write(outputBuffer)
  30.             swOut.Close()
  31.             Dim resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
  32.             Dim sr As New StreamReader(resp.GetResponseStream())
  33.             buffer = sr.ReadToEnd()
  34.             sr.Close()
  35.         Catch exp As Exception
  36.             buffer = "Errore: " & exp.Message.ToString()
  37.         End Try
  38.  
  39.         Return (buffer)
  40.     End Function
  41.  
  42.     Private Sub Download_Video(ByVal url As String)
  43.         Dim buffer As String = GetContent(url)
  44.         If buffer.IndexOf("Errore:") < 0 Then
  45.             Dim start As Integer = 0, [end] As Integer = 0
  46.             Dim startTag As String = "/watch_fullscreen?"
  47.             Dim endTag As String = ";"
  48.             start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase)
  49.             [end] = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase)
  50.             Dim str As String = buffer.Substring(start + startTag.Length, [end] - (start + startTag.Length))
  51.             Dim vid As String = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id"))
  52.             Dim l As String = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l"))
  53.             Dim t As String = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t"))
  54.             Dim title As String = str.Substring(str.IndexOf("&title=") + 7)
  55.             title = title.Substring(0, title.Length - 1)
  56.             lblvideo.Text = title
  57.             TCP.DownloadFileAsync(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))
  58.         End If
  59.     End Sub
  60.  
  61.     Private Sub DownloadComplete(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs) Handles TCP.DownloadFileCompleted
  62.         If e.Cancelled = True Then
  63.             MsgBox("I/Il download sono/è stati/o annullati/o", MsgBoxStyle.Exclamation)
  64.         Else
  65.             MsgBox("I/Il download sono/è stati/o completati/o", MsgBoxStyle.Information)
  66.         End If
  67.     End Sub
  68.  
  69.     Private Sub PrgChngd(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles TCP.DownloadProgressChanged
  70.         pbdownloadvideo.Value = e.ProgressPercentage()
  71.         pbtotaldownload.Value = (((total - downloades) / total) * 100)
  72.     End Sub
  73.  
  74.     Private Sub frmDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  75.         TDownload.Start()
  76.         Control.CheckForIllegalCrossThreadCalls = False
  77.         total = lstVideo.Items.Count
  78.     End Sub
  79.  
  80.     Private Sub TCoda() 'Thread di estrazione dalla coda
  81.         Dim url As String = ""
  82.         While True 'Ciclo infinito
  83.             If lstVideo.Items.Count > 0 Then
  84.                 url = lstVideo.Items(0).ToString() 'Ottengo l'url
  85.                 lstVideo.Items.RemoveAt(0) 'Rimuovo l'elemento dalla ListBox dei download
  86.                 downloades = total - lstVideo.Items.Count
  87.                 Download_Video(url) 'Mi raccomando, il download deve essere sincrono, non asincrono
  88.             End If
  89.             Threading.Thread.Sleep(500) 'Diamo un attimo di respiro al pc
  90.         End While
  91.     End Sub
  92.  
  93.     Private Sub cmdexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdexit.Click
  94.         TCP.CancelAsync()
  95.         Me.Close()
  96.     End Sub



é un pò lunghino...:rotfl:

PM Quote
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 16:25
Martedì, 25/08/2009
Non ho letto tutto ma credo che il problema sia quì:

TCP.DownloadFileAsync(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))

Il file lo devi scaricare in maniera sincrona, non asincrona, e cioè:

TCP.DownloadFile(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))

Ultima modifica effettuata da manvb.net il 25/08/2009 alle 16:26
PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 16:31
Martedì, 25/08/2009
Testo quotato

Postato originariamente da manvb.net:

Non ho letto tutto ma credo che il problema sia quì:

TCP.DownloadFileAsync(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))

Il file lo devi scaricare in maniera sincrona, non asincrona, e cioè:

TCP.DownloadFile(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))  



Non è quello...:d:d

PM Quote
Pagine: [ 1 2 3 4 ] Precedente | Prossimo