Export (0) Print
Expand All

Thread Class

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

For a list of all members of this type, see Thread Members.

System.Object
   System.Threading.Thread

[Visual Basic]
NotInheritable Public Class Thread
[C#]
public sealed class Thread
[C++]
public __gc __sealed class Thread
[JScript]
public class Thread

Thread Safety

This type is safe for multithreaded operations.

Remarks

A process can create one or more threads to execute a portion of the program code associated with the process. Use a ThreadStart delegate to specify the program code executed by a thread.

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.

Note   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.

Example

[Visual Basic, C#, C++] The following code example demonstrates simple threading functionality.

[Visual Basic] 
Imports System
Imports System.Threading

' Simple threading scenario:  Start a Shared method running
' on a second thread.
Public Class ThreadExample
    ' The ThreadProc method is called when the thread starts.
    ' It loops ten times, writing to the console and yielding 
    ' the rest of its time slice each time, and then ends.
    Public Shared Sub ThreadProc()
        Dim i As Integer
        For i = 0 To 9
            Console.WriteLine("ThreadProc: {0}", i)
            ' Yield the rest of the time slice.
            Thread.Sleep(0)
        Next
    End Sub

    Public Shared Sub Main()
        Console.WriteLine("Main thread: Start a second thread.")
        ' The constructor for the Thread class requires a ThreadStart 
        ' delegate.  The Visual Basic AddressOf operator creates this
        ' delegate for you.
        Dim t As New Thread(AddressOf ThreadProc)
        ' Start ThreadProc.  On a uniprocessor, the thread does not get 
        ' any processor time until the main thread yields.  Uncomment 
        ' the Thread.Sleep that follows t.Start() to see the difference.
        t.Start()
        'Thread.Sleep(0)

        Dim i As Integer
        For i = 1 To 4
            Console.WriteLine("Main thread: Do some work.")
            Thread.Sleep(0)
        Next

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.")
        Console.ReadLine()
    End Sub
End Class

[C#] 
using System;
using System.Threading;

// Simple threading scenario:  Start a static method running
// on a second thread.
public class ThreadExample {
    // The ThreadProc method is called when the thread starts.
    // It loops ten times, writing to the console and yielding 
    // the rest of its time slice each time, and then ends.
    public static void ThreadProc() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", i);
            // Yield the rest of the time slice.
            Thread.Sleep(0);
        }
    }

    public static void Main() {
        Console.WriteLine("Main thread: Start a second thread.");
        // The constructor for the Thread class requires a ThreadStart 
        // delegate that represents the method to be executed on the 
        // thread.  C# simplifies the creation of this delegate.
        Thread t = new Thread(new ThreadStart(ThreadProc));
        // Start ThreadProc.  On a uniprocessor, the thread does not get 
        // any processor time until the main thread yields.  Uncomment 
        // the Thread.Sleep that follows t.Start() to see the difference.
        t.Start();
        //Thread.Sleep(0);

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: Do some work.");
            Thread.Sleep(0);
        }

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
        Console.ReadLine();
    }
}

[C++] 

// [C++]
// Compile using /clr option.
#using <mscorlib.dll>
 using namespace System;
 using namespace System::Threading;
 // Simple threading scenario:  Start a Shared method running
 // on a second thread.
 public __gc class ThreadExample 
 {
 public:
     // The ThreadProc method is called when the thread starts.
     // It loops ten times, writing to the console and yielding 
     // the rest of its time slice each time, and then ends.
     static void ThreadProc()
     {
         for (int i = 0; i < 10; i++) 
         {
             Console::Write("ThreadProc: ");
             Console::WriteLine(i);
             // Yield the rest of the time slice.
             Thread::Sleep(0);
         }
     }
 };
 
 int main() 
 {
     Console::WriteLine(S"Main thread: Start a second thread.");
     // Create the thread, passing a ThreadStart delegate that
     // represents the ThreadExample::ThreadProc method.  For a 
     // delegate representing a static method, no object is
     // required.
     Thread *oThread = new Thread(new ThreadStart(0, &ThreadExample::ThreadProc));
 
     // Start the thread.  On a uniprocessor, the thread does not get 
     // any processor time until the main thread yields.  Uncomment
     // the Thread.Sleep that follows t.Start() to see the difference.
     oThread->Start();
     //Thread::Sleep(0);

     for (int i = 0; i < 4; i++) {
         Console::WriteLine("Main thread: Do some work.");
         Thread::Sleep(0);
     }

     Console::WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
     oThread->Join();
     Console::WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
     Console::ReadLine();
     return 0;
 }

[Visual Basic, C#, C++] 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.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Threading

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Thread Members | System.Threading Namespace | Threads and Threading | Using Threads and Threading

Show:
© 2014 Microsoft