Retrieving a Task’s Result

Because tasks can return a value as the result of background computation, an obvious question is how to retrieve the result. This section examines one way, which leaves the Task<TResult>-based world of asynchrony and blocks until the result is available. Later in this chapter, we discuss how to schedule more work based on a task’s result, using the concept of continuations (see the “Continuations” section).

Here’s how to retrieve a task’s result: Just call the Result property. This causes the receiving thread to block until the result is available:

images

The most important part of the sample code shown here is, likely, the ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.