Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27186

VS 2008 what am i doing wrong here?

$
0
0
The download work fine..the problem is if i click on the form1 to say drag it off to the side as long as i have the mouse down the thread stops running..i noticed the same thing with the listview scrollbar..what am i missing?

Code:

Private Delegate Sub SetResumeDownloadDelegate()
    Private Sub ResumeDownload()
        If Not InvokeRequired Then
            Try
                Dim URI As String = SearchLinka
                Dim startPointInt As Integer = Convert.ToInt32(startPoint) ' Put the object argument into an int variable
                Dim Request As HttpWebRequest = CType(HttpWebRequest.Create(URI), HttpWebRequest)
                Request.AddRange(startPointInt)
                Using Response As WebResponse = CType(Request.GetResponse, WebResponse)
                    Using responseStream As IO.Stream = Response.GetResponseStream
                        fileSize = Response.ContentLength
 
                        ' Dim Dir = My.Computer.FileSystem.GetParentPath(SaveLocation)
                        ' IO.Directory.CreateDirectory(Dir)
                        ' Dim CreateFile = File.Create(SaveLocation)
                        ' CreateFile.Close()
 
                      Using fs As New IO.FileStream(SaveLocation, FileMode.Append, FileAccess.Write)
                            Dim buffer As Byte() = New Byte(2047) {}
                            Dim read As Integer
                            Do
                                read = responseStream.Read(buffer, 0, buffer.Length)
                                fs.Write(buffer, 0, read) ' Write the data from the buffer to the local hard drive
                                Me.Invoke(New UpdateProgessCallback(AddressOf Me.UpdateProgress), New Object() {fs.Length, FileSize + startPointInt, Item})
                            Loop Until read = 0
                            responseStream.Close()
                            fs.Flush()
                            fs.Close()
                        End Using
                        responseStream.Close()
                    End Using
                    Response.Close()
                End Using
            Catch
            End Try
        Else
            Invoke(New SetResumeDownloadDelegate(AddressOf ResumeDownload), New Object() {})
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        StartDownloads()
    End Sub

    Public Sub StartDownloads()
        Dim Lview = ListView1
        For Each item As ListViewItem In Lview.CheckedItems
            With item
                Dim FileName As String = .SubItems(8).Text
                SaveLocation = "C:\TestDir\" & FileName
                SearchLinka = "http://Some Server address/" & FileName
                Start()
            End With
        Next
    End Sub
    Private Sub Start()
        t = New Thread(New System.Threading.ThreadStart(AddressOf ResumeDownload))
        If t.ThreadState = ThreadState.Unstarted Then
            ' The thread has never been started. Start it.
            t.IsBackground = True
            t.Start()
        Else
            ' The thread is paused. Resume it.
            ResumeDownload()
        End If
        Application.DoEvents()
    End Sub


Viewing all articles
Browse latest Browse all 27186

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>