How to cancel FTP?

CyborgPrime

Member
Joined
Sep 3, 2021
Messages
18
Programming Experience
10+
Hi! I wrote a cool updater - it works fine, but I have it set to download a bunch of files from an ftp site and I want to be able to cancel it in case the user wants to abort before the ftp is done.

I have it set to do the download after I click a button. I was hoping I could have a cancel button that stops the ftp loop.

Originally I wanted the DOWNLOAD button to change to CANCEL and be active again but it turns off until it's done processing the file list.

Any suggestions?
 
And as a quick aside, some of your internet searches may lead you to find some very bad advice from former VB6 programmers (and later VB.NET programmers) telling you that if your UI is freezing/hanging, you should put in some calls to Application.DoEvents(). DO NOT DO THIS. On the surface it may look like it solves your problems, but what is actually happening is that you are opening yourself up to world of pain trying to debug unusual re-entrancy problems, specially if your code is not written to handle re-entrancy. These bugs are really hard to reproduce and debug. Again, do not do this.
 
Ah! I had it set up that way in the first version but changed it after doing some web research. Being the first time doing file transfers over the web, I couldn't discern good advice from bad.

I'll switch it back and see how that works - thanks again - I'll report back when it's done
 
Everything is working, but the textBox doesn't update in real time (but the progress meter does) and the cancel button and other UI objects are "locked" until the FTP is done.
Hopefully using async will fix that.

Screenshot 2021-09-04 183347.png
 
If you are updating the textbox at the same time as updating the progress bar, then both controls should be refreshed at the same rate.

Please show us your new code.
 
hey guys, thank you SO MUCH!
I made some mods this afternoon using DownloadFileTaskAsync and it worked GREAT.
The downloads appear to be much faster, the user doesn't have to use FTP (just a folder on the web server), the text box now updates properly when the progress bar updates and the buttons and other active areas remain usable during the downloads.

I'm going to implement a way to cancel now that the buttons can be cliked.

Thanks again for your help! I'll report back when the whole thing is working.
 
ok works great, but if I am going to move away from FTP I need a way to get the remote directory (I used FTp to do that).

I think I see a way of getting the remote file listing without using ftp - I'll give it a try and report back
 
Why do you need the remote directory list.

The standard implementation of software updates is there to be a master manifest file at a fixed URL or location. The manifest file contains the latest version number and one or more references to the files for that version. The master manifest is update each time there is a new version published. The updater reads the manifest and compares the version in the manifest with the current running version. If the manifest declares a newer version, then the updater knows that it is behind. When user initiates the downloads, the updater has the list of URLs or locations of where to find the newer version of the files.
 
Here's the download code but the waiting never happens:


C#:
                    WebClient wc = new WebClient();
                    try
                    {
                        await wc.DownloadFileTaskAsync(URL, localFilePath + localFileName);
                    }
                    catch (Exception e)
                    {
                        updateStatus("Failed to connect to " + URL + " :" + e.ToString() + ".");

                    }

Instead it downloads about 44KB of the file then starts the next one.. what am I doing wrong?
 
That doesn't look right. The point of the asynchronous call is let the current thread continue running, and doing other stuff until you absolutely need the results, at which point you await the task so that you can get its results. If you are just going to await a call the WebClient.DownloadFileTaskAsync(), you might as well just call the synchronous WebClient.DownloadFile().
 
Hmm well it seems that no matter what method I use to call a download I only get 44KB worth of data then it considers the file done and moves on to the next. Not sure what's going on here.
 
Back
Top Bottom