TaskWait Method  
 

Wait for an asynchronous task to complete.

Syntax

object.TaskWait ( [ TaskId ], [ Milliseconds ], [ TimeElapsed ], [ TaskError ] )

Parameters

TaskId
An optional integer value that specifies the unique identifier associated with a background task.
Milliseconds
An optional integer value that specifies the number of milliseconds to wait for the background task to complete.
TimeElapsed
An optional integer value passed by reference that will contain the elapsed time for the task in milliseconds when the method returns. If this information is not required, this parameter may be omitted. This parameter is ignored if the TaskId parameter is omitted.
TaskError
An optional integer value passed by reference that will contain the last error code for the task when the method returns. If this information is not required, this parameter may be omitted. This parameter is ignored if the TaskId parameter is omitted.

Return Value

A Boolean value that specifies if the task has completed. A return value of True specifies that the background task has completed. A return value of False specifies that the background task is active.

Remarks

The TaskWait method waits for the specified task to complete. If the TaskId parameter is omitted, this method will wait for all active tasks to complete. If a task ID is specified and the Milliseconds parameter is non-zero, this method will cause the current working thread to block until the task completes or the amount of time exceeds the number of milliseconds specified by the caller. If the Milliseconds parameter is zero, then this function will poll the status of the task and return immediately to the caller. If the Milliseconds parameter is omitted, then the method will wait an infinite period of time for the task to complete.

If the specified task has already completed at the time this method is called, the method will return immediately without causing the current thread to block. If the TimeElapsed parameter has been specified, it will contain the number of milliseconds that it took for the task to complete. If the TaskError parameter has been specified, it will contain the last error code value that was set by the worker thread before it terminated. If the TaskError value is zero, that means that the background task was successful and no error occurred. A non-zero value will indicate that the background task has failed.

You should not call this method from the main UI thread with a long timeout period to wait for a background task to complete. Windows messages will not be processed while this method is blocked waiting for the background task to complete, and this can cause your application to appear non-responsive to the end-user. If you have a GUI application and you need to determine if a background task has finished, create a timer to periodically call the TaskDone method. When it returns True (indicating that the task has completed), you can safely call TaskWait to obtain the elapsed time and last error code without blocking the current thread.

See Also

TaskCount Property, TaskList Property, TaskAbort Method, TaskDone Method