共用方式為


AsyncLocal<T> 類別

定義

表示對於指定的非同步控制流程為本機的環境資料,例如非同步方法。

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 或其他內容轉換時隱含變更。

建構函式

AsyncLocal<T>()

具現化不會接收變更告知的 AsyncLocal<T> 執行個體。

AsyncLocal<T>(Action<AsyncLocalValueChangedArgs<T>>)

具現化會接收變更告知的 AsyncLocal<T> 本機執行個體。

屬性

Value

取得或設定環境資料的值。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於