AsyncLocal<T> AsyncLocal<T> AsyncLocal<T> AsyncLocal<T> Class

定義

非同期メソッドなど、特定の非同期制御フローに対してローカルなアンビエント データを表します。Represents ambient data that is local to a given asynchronous control flow, such as an asynchronous method.

generic <typename T>
public ref class AsyncLocal sealed
public sealed class AsyncLocal<T>
type AsyncLocal<'T> = class
Public NotInheritable Class AsyncLocal(Of T)

型パラメーター

T

アンビエント データの型。The type of the ambient data.

継承
AsyncLocal<T>AsyncLocal<T>AsyncLocal<T>AsyncLocal<T>

次の例ではAsyncLocal<T> 、クラスを使用して、非同期フロー全体で文字列値を保持します。The following example uses the AsyncLocal<T> class to persist a string value across an asynchronous flow. また、とAsyncLocal<T> ThreadLocal<T>の使用方法も異なります。It also contrasts the use of AsyncLocal<T> with 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>があるため、インスタンスを使用してスレッド間でデータを保持できます。Because the task-based asynchronous programming model tends to abstract the use of threads, AsyncLocal<T> instances can be used to persist data across threads.

またAsyncLocal<T> 、クラスは、現在のスレッドに関連付けられている値が変更されたときに、オプションValueの通知を提供します。これは、プロパティの設定によって明示的に変更されたか、またはスレッドでawaitまたはその他のコンテキスト遷移。The AsyncLocal<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.

コンストラクター

AsyncLocal<T>() AsyncLocal<T>() AsyncLocal<T>() AsyncLocal<T>()

変更通知を受信しない AsyncLocal<T> インスタンスをインスタンス生成します。Instantiates an AsyncLocal<T> instance that does not receive change notifications.

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

変更通知を受信する AsyncLocal<T> ローカル インスタンスをインスタンス生成します。Instantiates an AsyncLocal<T> local instance that receives change notifications.

プロパティ

Value Value Value Value

アンビエント データの値を取得または設定します。Gets or sets the value of the ambient data.

メソッド

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

適用対象