Export (0) Print
Expand All

Thread Class

Creates and controls a thread, sets its priority, and gets its status.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

[<Sealed>]
[<ClassInterfaceAttribute(ClassInterfaceType.None)>]
[<ComVisibleAttribute(true)>]
type Thread =  
    class
        inherit CriticalFinalizerObject
        interface _Thread
    end

The Thread type exposes the following members.

  NameDescription
Public methodThread(ParameterizedThreadStart)Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryThread(ThreadStart)Initializes a new instance of the Thread class.
Public methodThread(ParameterizedThreadStart, Int32)Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started and specifying the maximum stack size for the thread.
Public methodSupported by the XNA FrameworkThread(ThreadStart, Int32)Initializes a new instance of the Thread class, specifying the maximum stack size for the thread.
Top

  NameDescription
Public propertyApartmentState Obsolete. Gets or sets the apartment state of this thread.
Public propertyStatic memberCurrentContextGets the current context in which the thread is executing.
Public propertySupported by Portable Class LibraryCurrentCultureGets or sets the culture for the current thread.
Public propertyStatic memberCurrentPrincipalGets or sets the thread's current principal (for role-based security).
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCurrentThreadGets the currently running thread.
Public propertySupported by Portable Class LibraryCurrentUICultureGets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.
Public propertyExecutionContextGets an ExecutionContext object that contains information about the various contexts of the current thread.
Public propertyIsAliveGets a value indicating the execution status of the current thread.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryIsBackgroundGets or sets a value indicating whether or not a thread is a background thread.
Public propertyIsThreadPoolThreadGets a value indicating whether or not a thread belongs to the managed thread pool.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryManagedThreadIdGets a unique identifier for the current managed thread.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNameGets or sets the name of the thread.
Public propertySupported by the XNA FrameworkPriorityGets or sets a value indicating the scheduling priority of a thread.
Public propertyThreadStateGets a value containing the states of the current thread.
Top

  NameDescription
Public methodSupported by the XNA FrameworkAbort()Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
Public methodSupported by the XNA FrameworkAbort(Object)Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. Calling this method usually terminates the thread.
Public methodStatic memberSupported by the XNA FrameworkAllocateDataSlotAllocates an unnamed data slot on all the threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
Public methodStatic memberSupported by the XNA FrameworkAllocateNamedDataSlotAllocates a named data slot on all threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
Public methodStatic memberBeginCriticalRegionNotifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.
Public methodStatic memberBeginThreadAffinityNotifies a host that managed code is about to execute instructions that depend on the identity of the current physical operating system thread.
Public methodDisableComObjectEagerCleanupTurns off automatic cleanup of runtime callable wrappers (RCW) for the current thread.
Public methodStatic memberEndCriticalRegionNotifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception are limited to the current task.
Public methodStatic memberEndThreadAffinityNotifies a host that managed code has finished executing instructions that depend on the identity of the current physical operating system thread.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeReleases all the resources used by the CriticalFinalizerObject class. (Inherited from CriticalFinalizerObject.)

In XNA Framework 3.0, this member is inherited from Object.Finalize().


In Portable Class Library Portable Class Library, this member is inherited from Object.Finalize().
Public methodStatic memberSupported by the XNA FrameworkFreeNamedDataSlotEliminates the association between a name and a slot, for all threads in the process. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
Public methodGetApartmentStateReturns an ApartmentState value indicating the apartment state.
Public methodGetCompressedStack Obsolete. Returns a CompressedStack object that can be used to capture the stack for the current thread.
Public methodStatic memberSupported by the XNA FrameworkGetDataRetrieves the value from the specified slot on the current thread, within the current thread's current domain. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
Public methodStatic memberSupported by the XNA FrameworkGetDomainReturns the current domain in which the current thread is running.
Public methodStatic memberGetDomainIDReturns a unique application domain identifier.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns a hash code for the current thread. (Overrides Object.GetHashCode().)

In XNA Framework 3.0, this member is inherited from Object.GetHashCode().


In Portable Class Library Portable Class Library, this member is inherited from Object.GetHashCode().
Public methodStatic memberSupported by the XNA FrameworkGetNamedDataSlotLooks up a named data slot. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInterruptInterrupts a thread that is in the WaitSleepJoin thread state.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryJoin()Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryJoin(Int32)Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.
Public methodJoin(TimeSpan)Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryMemoryBarrierSynchronizes memory access as follows: The processor executing the current thread cannot reorder instructions in such a way that memory accesses prior to the call to MemoryBarrier execute after memory accesses that follow the call to MemoryBarrier.
Public methodStatic memberResetAbortCancels an Abort requested for the current thread.
Public methodResume Obsolete. Resumes a thread that has been suspended.
Public methodSetApartmentStateSets the apartment state of a thread before it is started.
Public methodSetCompressedStack Obsolete. Applies a captured CompressedStack to the current thread.
Public methodStatic memberSupported by the XNA FrameworkSetDataSets the data in the specified slot on the currently running thread, for that thread's current domain. For better performance, use fields marked with the ThreadStaticAttribute attribute instead.
Public methodSupported by the XNA FrameworkSetProcessorAffinityIn the .NET Compact Framework for Xbox 360, sets the processor affinity for a managed thread. Processor affinity determines the processors on which a thread runs.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySleep(Int32)Suspends the current thread for a specified time.
Public methodStatic memberSleep(TimeSpan)Blocks the current thread for a specified time.
Public methodStatic memberSpinWaitCauses a thread to wait the number of times defined by the iterations parameter.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryStart()Causes the operating system to change the state of the current instance to ThreadState.Running.
Public methodStart(Object)Causes the operating system to change the state of the current instance to ThreadState.Running, and optionally supplies an object containing data to be used by the method the thread executes.
Public methodSuspend Obsolete. Either suspends the thread, or if the thread is already suspended, has no effect.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodTrySetApartmentStateSets the apartment state of a thread before it is started.
Public methodStatic memberVolatileRead(Byte)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(Double)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(Int16)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(Int32)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(Int64)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(IntPtr)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(Object)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(SByte)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(Single)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(UInt16)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(UInt32)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(UInt64)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileRead(UIntPtr)Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.
Public methodStatic memberVolatileWrite(Byte, Byte)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(Double, Double)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(Int16, Int16)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(Int32, Int32)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(Int64, Int64)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(IntPtr, IntPtr)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(Object, Object)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(SByte, SByte)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(Single, Single)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(UInt16, UInt16)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(UInt32, UInt32)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(UInt64, UInt64)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberVolatileWrite(UIntPtr, UIntPtr)Writes a value to a field immediately, so that the value is visible to all processors in the computer.
Public methodStatic memberYieldCauses the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to.
Top

  NameDescription
Explicit interface implemetationPrivate method_Thread.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers.
Explicit interface implemetationPrivate method_Thread.GetTypeInfoRetrieves the type information for an object, which can then be used to get the type information for an interface.
Explicit interface implemetationPrivate method_Thread.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1).
Explicit interface implemetationPrivate method_Thread.InvokeProvides access to properties and methods exposed by an object.
Top

A process can create one or more threads to execute a portion of the program code associated with the process. Use a ThreadStart delegate or the ParameterizedThreadStart delegate to specify the program code executed by a thread. The ParameterizedThreadStart delegate allows you to pass data to the thread procedure.

For the duration of its existence, a thread is always in one or more of the states defined by ThreadState. A scheduling priority level, as defined by ThreadPriority, can be requested for a thread, but is not guaranteed to be honored by the operating system.

GetHashCode provides identification for managed threads. For the lifetime of your thread, it will not collide with the value from any other thread, regardless of the application domain from which you obtain the value.

NoteNote

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the CLR Hosting API to schedule many managed threads against the same operating system thread, or to move a managed thread between different operating system threads.

It is not necessary to retain a reference to a Thread object once you have started the thread. The thread continues to execute until the thread procedure is complete.

Important noteImportant

Beginning with the .NET Framework version 4, the behavior of some thread constructors is changed: Only fully trusted code can set the maximum stack size to a value that is greater than the default stack size (1 megabyte). If a larger value is specified when code is running with partial trust, the larger value is ignored and the default stack size is used. No exception is thrown. Code at any trust level can set the maximum stack size to a value that is less than the default stack size.

The following code example demonstrates simple threading functionality.

No code example is currently available or this language may not be supported.

This code produces output similar to the following:

 [VB, C++, C#]
 Main thread: Start a second thread.
 Main thread: Do some work.
 ThreadProc: 0
 Main thread: Do some work.
 ThreadProc: 1
 Main thread: Do some work.
 ThreadProc: 2
 Main thread: Do some work.
 ThreadProc: 3
 Main thread: Call Join(), to wait until ThreadProc ends.
 ThreadProc: 4
 ThreadProc: 5
 ThreadProc: 6
 ThreadProc: 7
 ThreadProc: 8
 ThreadProc: 9
 Main thread: ThreadProc.Join has returned.  Press Enter to end program.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft