let the task run in the background while waiting for the value
You can wait on a result in few different ways:
- Call
Task.Value
which will block until the value is computed. - Call
Task.IsDone
periodically, then callTask.Value
whenIsDone
returnsTrue
. - Call
Task.TryValue
periodically. - Get the value in the termination (
OnTerminated
) handler.
if an exception is raised, I want the main thread to catch it
Exception will be automatically forwarded to the point where your code reads the future result. As you are not reading the result anywhere, you can simply use if assigned(Task.FatalException)
in the OnTerminated
handler. (BTW, IsDone
will always be true in the termination handler.)
allow the main thread to determine if the task was completed or cancelled
Use Task.IsCancelled
.
All that is documented in the Future chapter of the Parallel Programming with the OmniThreadLibrary book.