Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim t = Task(Of Integer).Factory.StartNew(Function()
Dim max As Integer = 1000000
Dim ctr As Integer
For ctr = 0 to max
If ctr = max \ 2 And Date.Now.Hour <= 12 Then
ctr += 1
Exit For
End If
Return ctr
End Function)
Console.WriteLine("Finished {0:N0} iterations.", t.Result)
End Sub
End Module
' The example displays output like the following:
' Finished 1,000,001 iterations
For more complete examples, see
Task-based Asynchronous Programming
.
The
Task<TResult>
class also provides constructors that initialize the task but that do not schedule it for execution. For performance reasons, the
Task.Run
and
Task.Factory.StartNew
methods are the preferred mechanisms for creating and scheduling computational tasks, but for scenarios where task creation and scheduling must be separated, the constructors may be used, and the task's
Start
method may then be used to schedule the task for execution at a later time.
Starting with desktop apps that target the .NET Framework 4.6, the culture of the thread that creates and invokes a task becomes part of the thread's context. That is, regardless of the current culture of the thread on which the task executes, the current culture of the task is the culture of the calling thread. For apps that target versions of the .NET Framework prior to the .NET Framework 4.6, the culture of the task is the culture of the thread on which the task executes. For more information, see the "Culture and task-based asynchronous operations" section in the
CultureInfo
topic. Note that Store apps follow the Windows Runtime in setting and getting the default culture.
For operations that do not return a value, you use the
Task
class. Starting with C# 7.0, for a more lightweight task that is a value type rather than a reference type, use the
System.Threading.Tasks.ValueTask<TResult>
structure.
Gets the
AggregateException
that caused the
Task
to end prematurely. If the
Task
completed successfully or has not yet thrown any exceptions, this will return
null
.
(Inherited from
Task
)
Creates a continuation that receives caller-supplied state information and executes when the target
Task
completes.
(Inherited from
Task
)
Creates a continuation that receives caller-supplied state information and a cancellation token and that executes asynchronously when the target
Task
completes.
(Inherited from
Task
)
Creates a continuation that receives caller-supplied state information and a cancellation token and that executes when the target
Task
completes. The continuation executes based on a set of specified conditions and uses a specified scheduler.
(Inherited from
Task
)
Creates a continuation that receives caller-supplied state information and executes when the target
Task
completes. The continuation executes based on a set of specified conditions.
(Inherited from
Task
)
Creates a continuation that receives caller-supplied state information and executes asynchronously when the target
Task
completes. The continuation uses a specified scheduler.
(Inherited from
Task
)
Creates a continuation that receives a cancellation token and executes asynchronously when the target
Task
completes.
(Inherited from
Task
)
Creates a continuation that executes when the target task competes according to the specified
TaskContinuationOptions
. The continuation receives a cancellation token and uses a specified scheduler.
(Inherited from
Task
)
Creates a continuation that executes when the target task completes according to the specified
TaskContinuationOptions
.
(Inherited from
Task
)
Creates a continuation that executes asynchronously when the target
Task
completes. The continuation uses a specified scheduler.
(Inherited from
Task
)
Creates a continuation that receives caller-supplied state information and executes asynchronously when the target
Task
completes and returns a value.
(Inherited from
Task
)
Creates a continuation that executes asynchronously when the target
Task
completes and returns a value. The continuation receives caller-supplied state information and a cancellation token.
(Inherited from
Task
)
Creates a continuation that executes based on the specified task continuation options when the target
Task
completes and returns a value. The continuation receives caller-supplied state information and a cancellation token and uses the specified scheduler.
(Inherited from
Task
)
Creates a continuation that executes based on the specified task continuation options when the target
Task
completes. The continuation receives caller-supplied state information.
(Inherited from
Task
)
Creates a continuation that executes asynchronously when the target
Task
completes. The continuation receives caller-supplied state information and uses a specified scheduler.
(Inherited from
Task
)
Creates a continuation that executes asynchronously when the target
Task<TResult>
completes and returns a value.
(Inherited from
Task
)
Creates a continuation that executes asynchronously when the target
Task
completes and returns a value. The continuation receives a cancellation token.
(Inherited from
Task
)
Creates a continuation that executes according to the specified continuation options and returns a value. The continuation is passed a cancellation token and uses a specified scheduler.
(Inherited from
Task
)
Creates a continuation that executes according to the specified continuation options and returns a value.
(Inherited from
Task
)
Creates a continuation that executes asynchronously when the target
Task
completes and returns a value. The continuation uses a specified scheduler.
(Inherited from
Task
)
Waits for the
Task
to complete execution. The wait terminates if a cancellation token is canceled before the task completes.
(Inherited from
Task
)
Waits for the
Task
to complete execution. The wait terminates if a timeout interval elapses or a cancellation token is canceled before the task completes.
(Inherited from
Task
)
Gets a
Task
that will complete when this
Task
completes or when the specified
CancellationToken
has cancellation requested.
(Inherited from
Task
)
Gets a
Task
that will complete when this
Task
completes or when the specified timeout expires.
(Inherited from
Task
)
Gets a
Task
that will complete when this
Task
completes, when the specified timeout expires, or when the specified
CancellationToken
has cancellation requested.
(Inherited from
Task
)
Thread Safety
All members of
Task<TResult>
, except for
Dispose()
, are thread-safe and may be used from multiple threads concurrently.
See also
Task Parallel Library (TPL)
Task-based Asynchronous Programming
Samples for Parallel Programming with the .NET Core and .NET Standard