Abbruch eines laufenden FTP Downloads

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von joniator.

    Abbruch eines laufenden FTP Downloads

    Hallo,

    ich habe mich heute zum ersten Mal an VB.net rangetraut. Komme normalerweise eher aus den Scriptsprachen wie Javascript und PHP und tu mich noch etwas schwer.
    Einige kleine Anwendungen habe ich Autoit umgesetzt, was aber schnell an seine Grenzen stößt. Nun versuche ich einige Projekte auf VB.NET umzuziehen.

    Ich schreibe einen Downloader, der anhand von zwei eingegeben Kriterien eine INI-Datei auf dem FTP ausliest, die widerum die echten Downloadinfos enthält.
    Funktioniert wie ne 1 :)
    Sobald ich den Download-Button drücke, ändert der seinen Text in Cancel

    Der Download klappt und auch die Progressbar wird richtig geupdated.

    Nun ist die Anwendung aber so krass mit dem Download beschäftigt, dass ich den "neuen" Cancel button nicht mehr clicken kann.
    Der Download läuft in einem Loop.
    Ich weiß, ich hätte viel mehr in Funktionen auslagern können, aber das kommt danach, wenn endlich mal alles steht :)

    Hier der Code des Downloadbuttons:

    VB.NET-Quellcode

    1. Private Sub buttonDownload_Click(sender As Object, e As EventArgs) Handles buttonDownload.Click
    2. If buttonDownload.Text = "Download" Then
    3. Dim bAbortDownload = False
    4. buttonDownload.Text = "Cancel"
    5. buttonDownload.Update()
    6. Dim ftpHost As String = "ftp://irgendeinserver/"
    7. Dim ftpUser As String = "USER"
    8. Dim ftpPassword As String = "password"
    9. Dim ftpRemoteFile As String = "/" & textFilename.Text
    10. Dim ftpLocalFile As String = textFilename.Text
    11. 'MessageBox.Show(ftpRemoteFile, "bla", MessageBoxButtons.OK)
    12. Try
    13. Dim ftpSizeUrl As String = ftpHost & ftpRemoteFile
    14. Dim ftpSizeReq As FtpWebRequest = System.Net.FtpWebRequest.Create(ftpSizeUrl)
    15. ftpSizeReq.Credentials = New NetworkCredential(ftpUser, ftpPassword)
    16. ftpSizeReq.KeepAlive = False
    17. ftpSizeReq.Method = WebRequestMethods.Ftp.GetFileSize
    18. Dim ftpListResponse As FtpWebResponse = ftpSizeReq.GetResponse
    19. Dim ftpRemoteFileSize As Long = ftpListResponse.ContentLength
    20. barProgress.Maximum = ftpRemoteFileSize
    21. Dim ftpUrl As String = ftpHost & ftpRemoteFile
    22. Dim ftpReq As FtpWebRequest = System.Net.FtpWebRequest.Create(ftpUrl)
    23. ftpReq.Credentials = New NetworkCredential(ftpUser, ftpPassword)
    24. ftpReq.KeepAlive = False
    25. ftpReq.Credentials = New NetworkCredential(ftpUser, ftpPassword)
    26. ftpReq.KeepAlive = False
    27. ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
    28. Using ftpResp As System.Net.FtpWebResponse = CType(ftpReq.GetResponse, FtpWebResponse)
    29. Using ftpRespStream As Stream = ftpResp.GetResponseStream
    30. Using localFileStream As New FileStream(ftpLocalFile, FileMode.Create)
    31. Dim buffer(2047) As Byte
    32. Dim read As Integer = 0
    33. Do
    34. If bAbortDownload = True Then
    35. Exit Do
    36. End If
    37. read = ftpRespStream.Read(buffer, 0, buffer.Length)
    38. localFileStream.Write(buffer, 0, read)
    39. barProgress.Value = localFileStream.Length
    40. barProgress.Update()
    41. Loop Until read = 0
    42. ftpRespStream.Close()
    43. localFileStream.Flush()
    44. localFileStream.Close()
    45. End Using
    46. ftpRespStream.Close()
    47. End Using
    48. ftpResp.Close()
    49. End Using
    50. buttonDownload.Text = "Download"
    51. buttonDownload.Enabled = False
    52. buttonDownload.Update()
    53. Catch ex As Exception
    54. MessageBox.Show("error", "Error", MessageBoxButtons.OK)
    55. End Try
    56. Else
    57. Dim bAbortDownload = True
    58. buttonDownload.Text = "Download"
    59. buttonDownload.Enabled = False
    60. buttonDownload.Update()
    61. End If
    62. Return
    63. End Sub
    Das ganze Download-Geraffel in einen eigenen Thread auslagern.
    Wenn's dir nicht mehr gefällt kannst du den Thread mit Abort abbrechen oder alternativ die Abort-Variable beeinflussen.

    Am besten noch die ganze Struktur in eine eigene Klasse verlagern, die du mehrfach instantiieren kannst.

    Edit: oder s.u.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    Erzeuge einen downloader im form mit eventen sieht so aus:

    VB.NET-Quellcode

    1. Public Class Form1
    2. WithEvents d As New Net.WebClient

    Danach ladest du asyncron die datei runter, und wen du es cancelen willst:

    VB.NET-Quellcode

    1. d.CancelAsync()

    Diese methode bringt dir mehrere vorteile, z.b: Du kannst auch den downloadfortschritt verfolgen.
    Google mal "vb.net download async" es gibt tausende tuts dazu :D
    MFG
    Wer Rechtschreibfehler findet darf sie behalten :)