waitfortask
TASKID

The waitfortask statement temporarily pauses a procedure until the specified task is complete.


Parameters

This statement has one parameter:

taskid – the ID of the task to wait for (either a timer or a urltask().


Description

This statement temporarily pauses a procedure until the specified task is complete. The code for the task must contain a resumeaftertask statement as its final statement. The resumeaftertask statement should have the same ID as the task.

This statement is most commonly used with the urltask( function. In its simplest form, the statement and function can be paired directly together. In this example, a large file is downloaded from a web site, and then uncompressed.

waitfortask urltask("http://www.somesite.com/somebigfile.zip",
    "file","~/Documents/Test/SomeBigFile.zip",
    "code",{resumeaftertask «taskid»})
uncompress "~/Documents/Test/SomeBigFile.zip"
nsnotify "Download Finished and Uncompressed"

Because this code uses the urltask( function, Panorama does not “hang” while the file is downloaded. Even if it takes several minutes to download, Panorama will be fully responsive the whole time. You can click on other windows, do whatever you want. When the download finishes, the code will resume with the first statement after the waitfortask function, in this case the uncompress statement.

Note that since the user can do anything while the task is running, your code cannot assume that Panorama is in the same state when the task resumes. There may be a different window open, or the database that triggered the procedure might not even be open at all any more. If the code relies on a particular window or database being active, it must test for that, and if necessary make necessary adjustments, for example with the window statement.

In the example above, the code associated with the task doesn’t do anything except resume the additional code. But the code can do more, as long as the final statement is the resumeaftertask statement. Here is a more complicated example that uses the openasyncprogresswindow statement to open a window to display the progress of the download as it proceeds.

let url = "http://www.somesite.com/somebigfile.zip",
openasyncprogresswindow initializedictionary(
    "CAPTION",replace(url,"http://","")
)
let finishWindowCode = getdictionaryvalue(asyncTaskInfo,"FINISHCODE")
let progressVariable = getdictionaryvalue(asyncTaskInfo,"PROGRESSVARIABLE")

waitfortask urltask(url,
    "file","~/Documents/Test/SomeBigFile.zip",
    "progress",progressVariable,
    "code",finishWindowCode+{ resumeaftertask «taskid»})
uncompress "~/Documents/Test/SomeBigFile.zip"
nsnotify "Download Finished and Uncompressed"

You don’t have to pair the waitfortask statement and urltask( on the same line, you can separate them but keep them connected with a variable like this.

let taskid = urltask(...)
...
... extra code before pausing
...
waitfortask taskid
...
... resume after url is downloaded
...

Normally there is no connection between the main procedure and the code that is running asynchronously, each have their own, separate local variables. However, the asynchronous code can use the setwaitinglocal statement to pass data back to the original procedure. See the setwaitinglocal help page to learn more.

Using WaitForTask with a Timer

It’s also possible to use the waitfortask statement with a timer. This code pauses for 30 seconds, then resumes. During the pause, Panorama is fully functional, you can click on other windows, perform whatever operations you want.

let delay = 30
let taskid = info("guid")
starttimer taskid,
    "code",{resumeaftertask }+quoted(taskid),
    "repeat",1,
    "next",supernow()+delay
waitfortask taskid
...
... resume after 30 second delay
...

Does the code above seem overly complicated for a simple delay? Yeah, we thought so too. So you can skip all that and just use the wait statement.

wait 30

If you’re the sort that likes to peek under the hood, you can view the source of the wait statement and see that it is actually written using the waitfortask and resumeaftertask statements.


See Also


History

VersionStatusNotes
10.2NewNew in this version.