How to apply URLTask(

Per the Help file:

This example saves the specified web page as a text file.

 urltask("http://www.somesite.com/somepage.html","file","~/Documents/Test/Page.txt")

But this can’t be used to save a text file as-is so I don’t see the point. The result needs to be loaded to a field or variable: SomeDestination = URLTask(.

That would load the result into SomeDestination which would then be saved through additional processes.

So what is it that I’m missing in order to simply save a file such as Page.txt?

This function returns a task ID as its result. The content of the web page, or whatever is being downloaded, is saved as a side effect of the function. In the example you have quoted, it will be saved in ~/Documents/Test/Page.txt. If the second parameter had been the word “variable”, it would have been placed in a variable named by the third parameter.

The download is asynchronous. The task ID is returned instantly, and the procedure continues to run without waiting for the download to be completed, so the remainder of the procedure should not assume the download is complete. The urltask( function will save the content once the download is complete, and optionally run additional code at that time.

1 Like

Dave’s explanation is perfect. The only thing I would add is that if you want to fetch a URL synchronously then use the url( function, which works the way you seem to be expecting, i.e. SomeDestination = url(.

Asynchronous is exactly what I want and, thanks to David, it now makes sense to me as to how it works. I’ve also added code for follow up actions.

Inch by inch…

In the snippet below, URLTask( is working and successfully downloading the desired files. Nice feature, BTW! But now I can’t get waitfortask to follow up. With waitfortask in the procedure, nothing happens beyond it. It might as well be stop. If I replace it with wait, subsequent actions do occur.

LetFileGlobal taskid = urltask(lvSource,"file",lvTarget,"code",{ShowVariables fgDL})
zlog lvTarget+"1"
waitfortask taskid
;wait 5
zlog lvTarget+"2"

According to the waitfortask documentation in the Help file

The code for the task must contain a resumeaftertask statement with the same ID as the final statement in the task.

So try changing that to

LetFileGlobal taskid = urltask(lvSource,"file",lvTarget,"code",{ShowVariables fgDL
resumeaftertask «taskid»})
1 Like

Gosh David, you’d think I’m not reading the Help and you are. Far from it, but after reviewing it several times I failed to pick up on that detail. Thanks.

If the remaining procedure won’t run until the asynchronous urltask is complete, followed by resumeaftertask, what is the advantage it over the synchronous url?

Is it that you can be doing other things (just not the rest of that procedure) in Panorama while the download is happening - like if it’s a long download?

In my limited world, if I want to download something from a URL, my next action would follow after the download is complete. And the stuff I’d download is so small it would be done quickly. So I’m trying to imagine a scenario for the asynchronous option.

In my situation, the user may click on one or more of a few buttons to download files to pre-determined locations. If the download is successful I want the program to take other actions. Some downloads may take several seconds and there’s no need for the user to wait.

Yep, that’s exactly the purpose of this. You could have a download that takes minutes. This allows the user to do other things while the download proceeds – but then run code to process the file after the download is complete.

Certainly there are many scenarios where you do want to stop everything until the download is complete. That’s why Panorama offers both options. The asynchronous option is trickier to program (often a LOT trickier), but where it makes sense it can be very nice for the user.

What if there is an error, like you’re not connected to the internet or there is a problem with the web server? In that case, the synchronous version could be stuck for as much as a minute. The user can’t do anything until the error times out. If you use the asynchronous version, Panorama will continue to operate normally even if there is an error.

For example, the Site License window mostly uses the asynchronous version when it gets information from the provue.com server. So it doesn’t hang if there is an error.

On the other hand, when doing record locking the Panorama Client communicates with the server synchronously. It really needs to stop and wait for the server response before doing anything else. So if there is an error, the user will be stuck for a while, but it can’t be helped.

1 Like