Page 1 of 1

Waiting for download to complete

Posted: Wed Nov 16, 2011 1:13 am
by Pebah
HI,

I'm using the following script to download a file. After the download I move on to other things, How do I modify this script to force LiveCode to wait until the download is completed before doing anything else besides updating a progress bar?

Currently the program moves on to other handlers that are contingent on the download being finished, which it isn't.

Thanks!

Peter


on downloadFile
   open stack "MITA_Updater"
   
   repeat until the lockscreen is "false"
      unlock screen
   end repeat
   
   updateStatus "Downloading the update..."
   
   load URL "http://artsinteractive.org/updates/MITA-1NEW.livecode" with message "downloadDone"
   
   libURLSetStatusCallback "myProgress",the long ID of scrollbar "Progress"
end downloadFile

on myProgress theObject,theStatus
   put theStatus into theObject
end myProgress

on downloadDone pUrl, pStatus
   put theURL & "MITA-1NEW.livecode" into thisURL
   
    if pStatus = "cached" then
          put url pUrl into url ("binfile:" & thisURL)
     end if
     unload url pUrl
end downloadDone

Re: Waiting for download to complete

Posted: Wed Nov 16, 2011 11:36 am
by Klaus
Hi Peter,

you can download files either blocking or NON blocking.
Your current method "load" (like LibURLDownloadToFile etc.) are non blocking so you can display a progressbar or something.

If you use the blocking way like:
...
put URL "http://artsinteractive.org/updates/MITA-1NEW.livecode" into url("binfile:yourfilehere.livecode")
if the result <> empty then
## an error occurred
end if
...
Then the script will halt until the download is finished (or until an error occurs 8) ) but you canot have a progressbar this way.
Maybe you can show an animated GIF or something but that's all. So you have the choice :D

Another option could be to set a global variable "isloading" or whatever, but then you need to check this in every handler/action
that the user might execute while the download is happening. Know what I mean?


Best

Klaus

Re: Waiting for download to complete

Posted: Wed Nov 16, 2011 6:17 pm
by Pebah
Yes, I understand.

I've sort of solved the problem by using a slightly different procedure. I've made my script (a download and file updater) a two-step method: the user downloads the file by clicking a button, then clicks another button to install when the download is complete. Before it was a single-button, which I see is not possible with an active progress bar.

Thanks.

Re: Waiting for download to complete

Posted: Thu Nov 17, 2011 6:21 pm
by jacque
Another way to handle it with a single button is to change your script so that nothing else is called until the download completes. You don't show the whole script so I'm not sure what your handler continues to do, but the solution is to put the remainder of the actions into the "downloadDone" handler. That way you can check to see if the download is completed, and if so, call the handler that does whatever should happen next.

Code: Select all

on downloadDone pUrl, pStatus
   put theURL & "MITA-1NEW.livecode" into thisURL
    if pStatus = "cached" then
          put url pUrl into url ("binfile:" & thisURL)
          --> DoMyNextThing HERE  <--
     end if
     unload url pUrl
end downloadDone