Hi, to prevent freezing GUI it is recomennded to use asynchronous variaiton of download operation.
If you are on .net 4.5, just switch the line:
FTP.Download("path_server", "download", ...)
to
await FTP.DownloadAsync("path_server", "download", ...)
and make the Download
metod async with async
key word. Also remove the Application.DoEvents
call from the TransferProgressChanged
handler.
This will await for asynchronous download.
If you are on .net 4.0, it is not such simple, but achievable.
I suggest you to alter Download
method like this:
Private Sub Download()
If Not Connect() Then
Return
End If
Try
FTP.DownloadAsync("path_server", "download", TraversalMode.Recursive, TransferMethod.Copy, ActionOnExistingFiles.OverwriteDifferentSize).ContinueWith(AddressOf Disconnect)
Catch ex As Exception ''# this exception occurs before the asynchronous operation started.
MessageBox.Show(ex.Message)
Disconnect()
End Try
End Sub
And add overloaded Disconnect
method with Task
parameter like this:
Private Sub Disconnect(ByVal operation As Task)
If operation.IsFaulted Then
MessageBox.Show(operation.Exception.ToString())
ElseIf operation.IsCanceled Then ''# this is set when the download was aborted by FTP.Abort() call.
MessageBox.Show("Download was canceled.")
End If
Disconnect()
End Sub
And also remove the Application.DoEvents
call.
If you are on earlier version of .net framework, let us know.