Results 1 to 7 of 7

Thread: Download structure fails

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Question Download structure fails

    Hello all.

    I am totally into structures at this point, mostly because of their simplicity in use.

    I am now working on a "download structure", which allows you to download and keep track on a file.

    This is the code:
    Code:
      Public Structure FileDownload
            Sub New(ByVal URL As String, ByVal DestFilePath As String)
                Me.URL = URL
                Me._percent = 0
                Me.DownloadSize = Net.WebRequest.Create(Me.URL).GetResponse.ContentLength
                Me.DownloadedSize = 0
                Me.Percent = 0
                Me.DestinationFile = DestFilePath
                Me.client = New Net.WebClient
            End Sub
            Sub StartDownload()
                client.DownloadFileAsync(New Uri(Me.URL), Me.DestinationFile)
            End Sub
            Sub StopDownload(ByVal DeleteFile As Boolean)
                On Error Resume Next
                Me.client.CancelAsync()
                If DeleteFile = True And System.IO.File.Exists(Me.DestinationFile) Then
                    SetAttr(Me.DestinationFile, FileAttribute.Normal)
                    System.IO.File.Delete(Me.DestinationFile)
                    Me.DownloadedSize = 0
                End If
                Me.Percent = 0
            End Sub
            Private _percent As Single
            Public Property Percent() As Single
                Get
                    If System.IO.File.Exists(Me.DestinationFile) And Me.DownloadSize <> 0 Then
                        Me._percent = Me.DownloadedSize * 100 / Me.DownloadSize
                    Else
                        Me._percent = 0
                    End If
                    Return Me._percent
                End Get
                Set(ByVal value As Single)
                    Me._percent = value
                End Set
            End Property
            Private _DownloadedSize As Long
            Public Property DownloadedSize() As Long
                Get
                    Try
                        Me._DownloadedSize = New System.IO.FileInfo(Me.DestinationFile).Length
                    Catch
                        Me._DownloadedSize = 0
                    End Try
                    Return Me._DownloadedSize
                End Get
                Set(ByVal value As Long)
                    Me._DownloadedSize = value
                End Set
            End Property
            Private client As Net.WebClient
            Public DestinationFile As String
            Public URL As String
            Public DownloadSize As Long
        End Structure
    It all seems to work fine using the following thread to download a file:
    Code:
    d = New FileDownload("http://www.myurl.com/url/to/download.exe", My.Computer.FileSystem.SpecialDirectories.Desktop & "\download.exe")      
    d.StartDownload()     
    Do While d.DownloadedSize < d.DownloadSize
             ProgressbarAdapter(d.Percent, d.DownloadedSize & "/" & d.DownloadSize & " (" & d.Percent & "&#37;)")
             Threading.Thread.Sleep(200) 
    Loop
    Now my big problem:
    The second time I download the file, the file is made but the downloaded bytes stay 0. I carefully delete the file and then start it again. Somehow it can't download the file, but no error is given. Only after a while it crashes.
    If I then restart it, it just downloads the file.

    Soo...what is the reason I can't download the same file to the same location twice while the program is running?

    This is really a weird bug

    EDIT:

    It ALWAYS happens when twice downloading the same file. Changing the output file won't solve it. Really need help at this point.
    Last edited by bergerkiller; May 25th, 2010 at 02:12 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width