本文整理汇总了VB.NET中System.Threading.AsyncLocal<T>类的典型用法代码示例。如果您正苦于以下问题:VB.NET AsyncLocal<T>类的具体用法?VB.NET AsyncLocal<T>怎么用?VB.NET AsyncLocal<T>使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AsyncLocal<T>类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
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
输出:
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 ''