title: Stop FTP file downloading
body: <p>This is my sample code run under thread to download file from ftp server. In that, if the user want to stop file download, i tried to abort the thread. If the control in the while loop, its hang up.</p>
  
  <pre><code>     How to close the binaryreader and Stream, when reader in the middle of stream
  
       Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
                 Using input As Stream = response.GetResponseStream()
                          Using reader As New BinaryReader(input)
                              Try
                                  Using writer As New BinaryWriter(File.Open(targetFI.FullName, FileMode.Create)) 'output)
                                      Dim buffer(2048) As Byte '= New Byte(2048)
                                      Dim count As Integer = reader.Read(buffer, 0, buffer.Length)
                                      While count &lt;&gt; 0
                                          writer.Write(buffer, 0, count)
                                          count = reader.Read(buffer, 0, buffer.Length)
                                     End While
                                      writer.Close()
                                  End Using
  
                              Catch ex As Exception
                                  'catch error and delete file only partially downloaded
                                  targetFI.Delete()
                                  'Throw
                                  ret = False
                              End Try
                              reader.Close()
                          End Using
  
                          input.Close()
                      End Using
  
              response.Close()
              End Using
  </code></pre>