ThreadLocal(Of T) Class
Provides thread-local storage of data.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | ThreadLocal(Of T)() | Initializes the ThreadLocal(Of T) instance. |
![]() | ThreadLocal(Of T)(Boolean) | Initializes the ThreadLocal(Of T) instance and specifies whether all values are accessible from any thread. |
![]() | ThreadLocal(Of T)(Func(Of T)) | Initializes the ThreadLocal(Of T) instance with the specified valueFactory function. |
![]() | ThreadLocal(Of T)(Func(Of T), Boolean) | Initializes the ThreadLocal(Of T) instance with the specified valueFactory function and a flag that indicates whether all values are accessible from any thread. |
| Name | Description | |
|---|---|---|
![]() | IsValueCreated | Gets whether Value is initialized on the current thread. |
![]() | Value | Gets or sets the value of this instance for the current thread. |
![]() | Values | Gets a list for all of the values currently stored by all of the threads that have accessed this instance. |
| Name | Description | |
|---|---|---|
![]() | Dispose() | Releases all resources used by the current instance of the ThreadLocal(Of T) class. |
![]() | Dispose(Boolean) | Releases the resources used by this ThreadLocal(Of T) instance. |
![]() | Equals(Object) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Releases the resources used by this ThreadLocal(Of T) instance.(Overrides Object.Finalize().) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | ToString() | Creates and returns a string representation of this instance for the current thread.(Overrides Object.ToString().) |
The following example shows how to use ThreadLocal(Of T):
Imports System.Threading Imports System.Threading.Tasks Module ThreadLocalDemo ' Demonstrates: ' ThreadLocal(T) constructor ' ThreadLocal(T).Value ' One usage of ThreadLocal(T) Sub Main() ' Thread-Local variable that yields a name for a thread Dim ThreadName As New ThreadLocal(Of String)( Function() Return "Thread" & Thread.CurrentThread.ManagedThreadId End Function) ' Action that prints out ThreadName for the current thread Dim action As Action = Sub() ' If ThreadName.IsValueCreated is true, it means that we are not the ' first action to run on this thread. Dim repeat As Boolean = ThreadName.IsValueCreated Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, If(repeat, "(repeat)", "")) End Sub ' Launch eight of them. On 4 cores or less, you should see some repeat ThreadNames Parallel.Invoke(action, action, action, action, action, action, action, action) ' Dispose when you are done ThreadName.Dispose() End Sub End Module
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
With the exception of Dispose, all public and protected members of ThreadLocal(Of T) are thread-safe and may be used concurrently from multiple threads. The value returned for the Value and IsValueCreated properties is specific for the thread on which the property is accessed.


