AsyncLocal(Of T) Class
Represents ambient data that is local to a given asynchronous control flow, such as an asynchronous method.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | AsyncLocal(Of T)() | Instantiates an AsyncLocal(Of T) instance that does not receive change notifications. |
![]() | AsyncLocal(Of T)(Action(Of AsyncLocalValueChangedArgs(Of T))) | Instantiates an AsyncLocal(Of T) local instance that receives change notifications. |
| Name | Description | |
|---|---|---|
![]() | Value | Gets or sets the value of the ambient data. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
Because the task-based asynchronous programming model tends to abstract the use of threads, AsyncLocal(Of T) instances can be used to persist data across threads.
The AsyncLocal(Of T) class also provides optional notifications when the value associated with the current thread changes, either because it was explicitly changed by setting the Value property, or implicitly changed when the thread encountered an await or other context transition.
The following example uses the AsyncLocal(Of T) class to persist a string value across an asynchronous flow. It also contrasts the use of AsyncLocal(Of T) with ThreadLocal(Of T).
Imports System.Threading Imports System.Threading.Tasks Module Example Dim _asyncLocalString As New AsyncLocal(Of String)() Dim _threadLocalString As New ThreadLocal(Of String)() Async Function AsyncMethodA() As Task ' Start multiple async method calls, with different AsyncLocal values. ' We also set ThreadLocal values, to demonstrate how the two mechanisms differ. _asyncLocalString.Value = "Value 1" _threadLocalString.Value = "Value 1" Dim t1 = AsyncMethodB("Value 1") _asyncLocalString.Value = "Value 2" _threadLocalString.Value = "Value 2" Dim t2 = AsyncMethodB("Value 2") ' Await both calls await t1 await t2 End Function Async Function AsyncMethodB(expectedValue As String) As Task Console.WriteLine("Entering AsyncMethodB.") Console.WriteLine(" Expected '{0}', AsyncLocal value is '{1}', ThreadLocal value is '{2}'", expectedValue, _asyncLocalString.Value, _threadLocalString.Value) await Task.Delay(100) Console.WriteLine("Exiting AsyncMethodB.") Console.WriteLine(" Expected '{0}', got '{1}', ThreadLocal value is '{2}'", expectedValue, _asyncLocalString.Value, _threadLocalString.Value) End Function Public Sub Main() AsyncMethodA.Wait() End Sub End Module ' The example displays the following output: ' Entering AsyncMethodB. ' Expected 'Value 1', AsyncLocal value is 'Value 1', ThreadLocal value is 'Value 1' ' Entering AsyncMethodB. ' Expected 'Value 2', AsyncLocal value is 'Value 2', ThreadLocal value is 'Value 2' ' Exiting AsyncMethodB. ' Expected 'Value 2', got 'Value 2', ThreadLocal value is '' ' Exiting AsyncMethodB. ' Expected 'Value 1', got 'Value 1', ThreadLocal value is ''
Available since 10
.NET Framework
Available since 4.6
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

