AsyncLocal<T> 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示对于给定异步控制流(如异步方法)是本地数据的环境数据。
generic <typename T>
public ref class AsyncLocal sealed
public sealed class AsyncLocal<T>
type AsyncLocal<'T> = class
Public NotInheritable Class AsyncLocal(Of T)
- T
环境数据的类型。
- 继承
-
AsyncLocal<T>
以下示例使用 AsyncLocal<T> 类在异步流中保留字符串值。 它还将 的用法 AsyncLocal<T> 与 进行 ThreadLocal<T>对比。
using System;
using System.Threading;
using System.Threading.Tasks;
class Example
{
static AsyncLocal<string> _asyncLocalString = new AsyncLocal<string>();
static ThreadLocal<string> _threadLocalString = new ThreadLocal<string>();
static async Task AsyncMethodA()
{
// 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";
var t1 = AsyncMethodB("Value 1");
_asyncLocalString.Value = "Value 2";
_threadLocalString.Value = "Value 2";
var t2 = AsyncMethodB("Value 2");
// Await both calls
await t1;
await t2;
}
static async Task AsyncMethodB(string expectedValue)
{
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);
}
static async Task Main(string[] args)
{
await AsyncMethodA();
}
}
// 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 ''
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 ''
由于基于任务的异步编程模型倾向于抽象化线程的使用, AsyncLocal<T> 因此实例可用于跨线程持久保存数据。
当与当前线程关联的值发生更改时,类 AsyncLocal<T> 还提供可选通知,可能是因为通过设置 Value 属性显式更改,或者在线程遇到 await
或其他上下文转换时隐式更改。
Async |
实例化不接收更改通知的 AsyncLocal<T> 实例。 |
Async |
实例化接收更改通知的 AsyncLocal<T> 本地实例。 |
Value |
获取或设置环境数据的值。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
Get |
作为默认哈希函数。 (继承自 Object) |
Get |
获取当前实例的 Type。 (继承自 Object) |
Memberwise |
创建当前 Object 的浅表副本。 (继承自 Object) |
To |
返回表示当前对象的字符串。 (继承自 Object) |
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |