Cancelling FTP Download with File Transfer Control


I am working on a FTP program and am trying to set it up so you can cancel a file while its being downloaded.  I am using the Internet Transfer Control in Visual Basic 6.  I tried the .Cancel action but it hangs for a long time.  I also tried to use the FTP command "ABOR" to abort it but I get a run time error that says a command is still executing.  Can somebody please give me some heads up on how to fix this?  Thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, nickfantis.

Try adding a "DoEvents" line item just before the section of your code the starts a download--that may allow your code to accept an interruption (e.g. the examples you mentioned above). If that doesn't help, one of the ideas in this EE post may help:
The problem your encountering is that the loop you are using is still executing and will not allow you to cancel.  If you have not set up any error handling you will not see this error.  The following code below is how I got it to cancel/abort a download. Also I used latebinding rather than adding the actual reference to the Microsoft Inet Control. So you don't need to add any reference's to your project when you test my code. Just copy and paste the code below into a form.

Option Explicit
'NOTE: I highly recommend running as an executable.  Inet will hang in the VB IDE _
'for about 15-30 seconds until it restores which can be annoying.
'CREATE 2 Command Buttons:
'cmdCommand1    caption = Download File
'cmdCommand2    caption = Abort / Cancel Download
'Set the files to download from FTP and location to save on hard disk.
Const LocalSourceFile = "PPTVIEW.EXE" 'Tested with 1.70 MB file
Const TargetName = "D:\PPTVIEW.EXE"
Const icFTP = 2           'Protocal constant
Public Inet1 As Object

Public Sub ftpDownloadFile(ftpURL As String, ftpUsername As String, ftpPass As String, ftpChangeDir As String)
On Error GoTo ftpErr
Set Inet1 = CreateObject("InetCtls.Inet")
With Inet1
      .Protocol = icFTP
      .URL = ftpURL
      .UserName = ftpUsername
      .Password = ftpPass
      .Execute .URL, ftpChangeDir
        Do Until .StillExecuting = False
      .Execute .URL, "GET " & LocalSourceFile & " " & TargetName
        Do Until .StillExecuting = False
   End With
   Set Inet1 = Nothing
    Exit Sub
Caption = Err.Number & "  " & Err.Description
Exit Sub
End Sub

Public Sub CancelDownload(doCancel As Boolean)
On Error GoTo myCancelErr
Select Case doCancel
Case False
Exit Sub

Case True
If Inet1.StillExecuting = True Then
         Inet1.Execute , "CLOSE"
        Do Until Inet1.StillExecuting = False
End If
         Set Inet1 = Nothing
        'File download aborted.
End Select
Caption = Err.Number & " " & Err.Description
End Sub

Private Sub cmdCommand1_Click()
'Your FTP settings
Call ftpDownloadFile("", "username", "password", "CD \")
End Sub

Private Sub cmdCommand2_Click()
'True Cancel Download
CancelDownload (True)
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'Clean up...
Set Inet1 = Nothing
Unload Me
End Sub

This is my shot at it..hope it works out for you :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nickfantisAuthor Commented:
I am still having problems getting it to stop hanging, even while running it compiled.  Here is the code snippet:

Private Sub cmdDisconnect_Click()

    lblCurrentStatus.ForeColor = vbYellow
    lblCurrentStatus.Caption = "Disconnecting..."
    Inet.Execute , "CLOSE"
    Loop Until Inet.StillExecuting = False
    lblCurrentStatus.ForeColor = vbRed
    lblCurrentStatus.Caption = "Disconnected"
    cmdConnect.Enabled = True
    cmdDisconnect.Enabled = False
    cmdMkdir.Enabled = False
    cmdDelete.Enabled = False
    cmdUpDir.Enabled = False
    cmdDownload.Enabled = False
End Sub

Here is the main code for the download button:

        Loop Until Inet.StillExecuting = False
        Dim strGet As String
        strGet = Chr(34) & lstvServer.SelectedItem.Text & Chr(34) & " " & Chr(34) & filLocalFile.Path & "\" & lstvServer.SelectedItem.Text & Chr(34)
        Inet.Execute , "GET " & strGet
        lblCurrentStatus.ForeColor = vbYellow
        lblCurrentStatus.Caption = "Downloading..."
        Loop Until Inet.StillExecuting = False

eql1044, thanks for your idea but going that route would require me to recode the entire program which I am trying to avoid.  I tried taking your idea from your code and applying it to mine but I am still getting it to hang.  

Thanks for your help, hopefully seeing my code can help me get a solution.
As I mentioned before it will hang until .EXECUTE has finished when you call the Do until loop it will contiunue until .Execute has finished until then the program will not close immediatley. You didn't really mention if the abort/cancel of the download is working for you now? is that working?
<<<<  thanks for your idea but going that route would require me to recode the entire program

Not really, All you really need to do is do a FIND and replace all on all the Inet1 and change it to Inet
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.