Creating Tasks That Return Values

The Task class also has a generic counterpart that you can use for creating tasks that return a value. Consider the following code snippet that creates a task returning a value of type Double, which is the result of calculating the tangent of an angle:

Dim taskWithResult = Task(Of Double).    Factory.StartNew(Function()                         Dim radians As Double _                             = 120 * Math.PI / 180                         Dim tan As Double = _                             Math.Tan(radians)                         Return tan                     End Function)

You use a Function, which represents a System.Func(Of T), so that you can return a value from your operation. ...

Get Visual Basic 2015 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.