本文整理汇总了VB.NET中System.Threading.Thread.AllocateDataSlot方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Thread.AllocateDataSlot方法的具体用法?VB.NET Thread.AllocateDataSlot怎么用?VB.NET Thread.AllocateDataSlot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Thread
的用法示例。
在下文中一共展示了Thread.AllocateDataSlot方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Main
' 导入命名空间
Imports System.Threading
Class Test
<MTAThread> _
Shared Sub Main()
For i As Integer = 1 To 3
Dim newThread As New Thread(AddressOf ThreadData.ThreadStaticDemo)
newThread.Start()
Next i
End Sub
End Class
Class ThreadData
<ThreadStatic> _
Shared threadSpecificData As Integer
Shared Sub ThreadStaticDemo()
' Store the managed thread id for each thread in the static
' variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId
' Allow other threads time to execute the same code, to show
' that the static data is unique to each thread.
Thread.Sleep( 1000 )
' Display the static data.
Console.WriteLine( "Data for managed thread {0}: {1}", _
Thread.CurrentThread.ManagedThreadId, threadSpecificData )
End Sub
End Class
输出:
Data for managed thread 4: 4 Data for managed thread 5: 5 Data for managed thread 3: 3
示例2: Test
' 导入命名空间
Imports System.Threading
Class Test
<MTAThread> _
Shared Sub Main()
Dim newThreads(3) As Thread
For i As Integer = 0 To newThreads.Length - 1
newThreads(i) = New Thread(AddressOf Slot.SlotTest)
newThreads(i).Start()
Next i
End Sub
End Class
Public Class Slot
Shared randomGenerator As Random
Shared localSlot As LocalDataStoreSlot
Shared Sub New()
randomGenerator = new Random()
localSlot = Thread.AllocateDataSlot()
End Sub
Shared Sub SlotTest()
' Set different data in each thread's data slot.
Thread.SetData(localSlot, randomGenerator.Next(1, 200))
' Write the data from each thread's data slot.
Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _
AppDomain.GetCurrentThreadId().ToString(), _
Thread.GetData(localSlot).ToString())
' Allow other threads time to execute SetData to show
' that a thread's data slot is unique to the thread.
Thread.Sleep(1000)
' Write the data from each thread's data slot.
Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _
AppDomain.GetCurrentThreadId().ToString(), _
Thread.GetData(localSlot).ToString())
End Sub
End Class