本文整理汇总了VB.NET中System.Threading.Thread.GetNamedDataSlot方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Thread.GetNamedDataSlot方法的具体用法?VB.NET Thread.GetNamedDataSlot怎么用?VB.NET Thread.GetNamedDataSlot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Thread
的用法示例。
在下文中一共展示了Thread.GetNamedDataSlot方法的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
Public Shared Sub Main()
Dim newThreads(3) As Thread
Dim i As Integer
For i = 0 To newThreads.Length - 1
newThreads(i) = _
New Thread(New ThreadStart(AddressOf Slot.SlotTest))
newThreads(i).Start()
Next i
Thread.Sleep(2000)
For i = 0 To newThreads.Length - 1
newThreads(i).Join()
Console.WriteLine("Thread_{0} finished.", _
newThreads(i).ManagedThreadId)
Next i
End Sub
End Class
Class Slot
Private Shared randomGenerator As New Random()
Public Shared Sub SlotTest()
' Set random data in each thread's data slot.
Dim slotData As Integer = randomGenerator.Next(1, 200)
Dim threadId As Integer = Thread.CurrentThread.ManagedThreadId
Thread.SetData(
Thread.GetNamedDataSlot("Random"),
slotData)
' Show what was saved in the thread's data slot.
Console.WriteLine("Data stored in thread_{0}'s data slot: {1,3}",
threadId, slotData)
' Allow other threads time to execute SetData to show
' that a thread's data slot is unique to itself.
Thread.Sleep(1000)
Dim newSlotData As Integer = _
CType(Thread.GetData(Thread.GetNamedDataSlot("Random")), Integer)
If newSlotData = slotData Then
Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}",
threadId, newSlotData)
Else
Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}",
threadId, newSlotData)
End If
End Sub
End Class