Progress bar on FTP download

Discussion in 'Questions (Windows Mobile)' started by willisgt, Jan 24, 2008.

  1. willisgt

    willisgt Active Member Licensed User

    Is there any way to associate a progress bar with an FTP file download?

    I've got a little utility that downloads files off my server with no problems, but I'd like to find a way to show the progress of the download (so I know if I've got time to go get coffee, etc...) :)

  2. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible as the download method is a synchronous operation.
    You can show the file size before download.
  3. dzt

    dzt Active Member Licensed User

    Yes but now there is agraham's Threading library

    You can run ftp commands in a seperate thread using Thread object.

    In your main thread add a timer and check the filesize of the file you are downloading

    FileSize(AppPath & "\")

    That's it!
  4. willisgt

    willisgt Active Member Licensed User

    Great idea, Dimitris. I'm having a small problem with the implementation, though.

    I put the code that updates the progress bar into its own thread; it just crashes. Really not sure why.

    Have you done anything like this sucessfully?
  5. agraham

    agraham Expert Licensed User

    Are you using thread events? I tried to explain in the help that you can't update graphic controls directly in code in a thread. You must fire a thread event to do the updating for you. See the example that updates a textbox from a thread - you must do the same.
  6. willisgt

    willisgt Active Member Licensed User

    I tried to have a look at the help file you provided before I satrted working with the .dll, but with no luck. The window hust diaplys 'Action Cancelled' whnever I pull up the 'Threading' help menu option.

    So I'm tying to work entirely from the example code you provided.

    Yes, I put the .chm file into /basic4ppc/libraries.

    (I've got to find some time to try to write a .dll - that's just so cool to be able to add on to Basic4PPC!)

  7. agraham

    agraham Expert Licensed User

    That's very strange. I use the same template for all my help files and just modify it each time so there should be nothing different with that particular help file. It works fine for me, and I assume dzt who has tried the library. Are any other help files affected? I suggest a Google for "Help Action cancelled" it bought up a couple of things to check.
  8. willisgt

    willisgt Active Member Licensed User

    Oops... I forgot that Windows XP's internal security can block files like this. The solution (for those who don't know) is to right-click the file and click 'Unblock'. Then the contents will display correctly.

    Oh, and the error I was getting was a divide by zero when calculating the progress bar's value. Had nothing to do with threading.

    Now if I can just find my 'dunce' cap...

    So now my code is error-free but does not update the progress bar; as you say, GUI operations are not thread-safe. So I'm still looking for a solution that will update the progress bar during an ftp file download.

    I'm a beleiver that there's always a solution.

    Great library, by the way!

  9. agraham

    agraham Expert Licensed User

    As the FTP thread will be blocked during file transfer because it is a synchronous operation there is no way to do it from that thread unless you are downloading mutiple files when you could update progress from that thread between files.

    I can't think of anyway of monitoring the progress of a synchronous process other than what dzt suggested in post #3 i.e. monitor something that is changing from the main or another thread thread. In this case, as he suggested, the size of the downloading file. However as the file is presumably open during the download I don't know if the returned size would be anything other than zero until it is closed - nor indeed whether it would locked and inaccessible. Only a trial would tell.
  10. dennishea

    dennishea Active Member Licensed User

    How does explorer display the size of the file to be downloaded and the amount thats downloaded so far? Thats what you are talking about, right?

  11. agraham

    agraham Expert Licensed User

    The answer to this is bit more complicated than you might think. The following is simplified, and perhaps not entirely correct, but hopefully it provides some sort of explanation.

    Many .NET/Operating System functions are available in both synchronous and asynchronous form. B4PPC offers only the synchronous forms for simplicity.

    Synchronous operations basically block the caller until they either succeed or fail, so they need no further management by the caller. Thus they are simple to use and that is why B4PPC restricts itself to them.

    Asynchronous operations in comparison need ongoing management by the caller. Typically they involve calling a function to get things going which launches a separate thread and registers a "callback function" or event that the thread calls to handle the completion of the operation. The caller is then free to do other things and when the completion event occurs must call a finalise function to get the data from the operation thread and tidy things up.

    Crucially however, in order to monitor progress the, in this case FTP, process must be written with the ability to report on its' progress, via another callback, if used asynchronously. The .NET FTP functions do not do this. Explorer is probably using either its' own FTP process built on top of the OS TCP/IP stack, or is using a lower level OS facility that does provide progress monitoring.
  12. dzt

    dzt Active Member Licensed User

    Before posting for the first time in this topic I had made a successful test (desktop only)
  13. RandomCoder

    RandomCoder Well-Known Member Licensed User

    It's just an idea and not very reliable but....
    If you know the size of the file to be downloaded and have a rough idea of the rate at which it transfers then you could just make a 'best guess' and use a timer to set the update of the progress bar.

  14. willisgt

    willisgt Active Member Licensed User

    Got it!

    Okay - first approach was to download the file in the main code and use the thread to update the progress bar. Didn't work for reasons explained previously in this thread.

    So i tried turning it around - if only the main thread can update the GUI, then use the main code to update the progress bar, and use the thread to download the file.


  15. agraham

    agraham Expert Licensed User

    Yes, I think this is what dzt intially suggested and what we thought you were trying to do. It never even crossed my mind that you were trying the other way round :sign0161:

  16. dennishea

    dennishea Active Member Licensed User

    Thanks Agraham for the reply. I think I have a grasp of what your saying.

  17. willisgt

    willisgt Active Member Licensed User

    Looking back, that's exactly what Dimitris suggested.

    Could've saved myself a lot of grief if I'd read it a little more closely (and fully grasped the problem at the outset).

    Guess that's one of those little gaps between concept and implementation...

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice