My program starts a download. Before moving on and opening and reading the file I downloaded, I have to wait to verify the download happened.
I am currently using a download timer to check every 100 ms whether a boolean is true or not. The boolean is set in the download method once it is downloaded.
The problem is that I have to call then action for the timer and then come back to the method and continue execution where it left off.
Anybody have any ideas on how I can wait in the method without having to call a timer to retriever the method after the download happened? I have also tested looping until the boolean is true but Im not sure whether thats the best method either.
Don’t try to “wait” within a method for an action to complete. Whatever you use will lock up your program. Just call a separate method to do what you need done when the download is complete.
However I end up calling the next part isn’t important. I just need to stop the execution before it moves on with the next step. How do I properly wait?
Use a Timer. The Timer action event calls the next step. Move all your code after you initiate the download into a method that the timer calls after the download is complete. Your current method will just start the download and return. Nothing happens until the timer detects the download is complete and calls the new method.
Tims solution is essentially what I proposed with the addition of a timer to delay the new method. Either way, you should not wait within a method. You should initiate a new method once your download completes.
Don’t wait in a method, unless you’re threading it off you will experience bad mojo ;), gloopy or unresponsive ui etc.
Change to an asynchronous request and trigger the next step in the HTTPSocket.DownloadComplete event or Xojo.Net.HTTPSocket.FileReceived event depending on which you’re using?
If you don’t want to go async, make a state machine. No external timers or properties needed, everything self contained, just call DoDownload.
Psudo code:
Method DoDownload
static state as integer = 0
if state = 0 then
start download
state = state + 1
else if state = 1 then
update progress
if progress is done then state = state + 1
else if state = 2 then
check file is ok
if file is ok then state = state + 1 or state = 0
else if state = 3 then
do something with file
state = state + 1
else if state = 4 then
delete file
state = state + 1
end if
if state > 4 then
xojo.core.timer.calllater(100, addressof DoDownload) 'keep calling this method if we've not finished
end if
You could use a Select instead of all those if’s if you wanted, personal preference
At any time you want to stop, just set state >4 and the method wont be run again.