NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Thread Class

Represents threads that execute within the runtime, to create and control threads.

Object
   Thread

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

Remarks

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

Requirements

Namespace: System.Threading

Assembly: mscorlib.dll

Example [Visual Basic]

Example: Simple

[C++]
#import <mscorlib.dll>
[managed]
class Foo 
{
public:
    //
    // The method that will be called when the thread is started
    //
    void Baz()
    {
        Console::WriteLine(L"Foo Baz is running on another thread");
    }
};

int main() 
{
    Console::WriteLine(L"Thread Simple Sample");
    Foo *oFoo = new Foo();

    //
    // Create the thread object, passing in the Foo::Baz method
    // via a ThreadStart delegate
    //
    Thread *oThread = new Thread(new ThreadStart(oFoo, &Foo::Baz));

    //
    // Start the thread
    //
    oThread->Start();

    return 0;
}

[Visual Basic]

Class Foo
' The method that will be called when the thread is started
Sub Baz() 
   Console.WriteLine("Foo Baz is running on another thread")
End Sub
End Class

Sub main()
   Console.WriteLine("Thread Simple Sample")
   Dim oFoo As Foo
   Set oFoo = New Foo()
' Create a ThreadStart object, passing the address of oFoo.Baz
' Note that VB is using syntactic sugar here. The constructor
' for ThreadStart actually takes two parameters: an Object
' and the address of the method body. 
   Dim otter As ThreadStart
   Set otter = New ThreadStart(AddressOf oFoo.Baz)
' Create a Thread object 
   Dim oThread As Thread
   Set oThread = New Thread(otter)
' Starting the thread invokes the ThreadStart delegate
   oThread.Start
End Sub

Example: StopJoin

[C++]
#import <mscorlib.dll> 
[managed] class Foo  
{  
public:  
    //  
    // The method that will be called when the thread is started  
    //  
    void Baz()  
    {  
         while (true)  
         { 
 Console::WriteLine(L"Foo Baz is running on another thread");  
         }  
    }  
}; 
  
int main()  
{  
    Console:: WriteLine(L"Thread Stop Join Sample"); 
    Foo *oFoo = new Foo(); 
    //  
    // Create the thread object, passing in the Foo::Baz method  
    // via a ThreadStart delegate  
    //  
    Thread *oThread1 = new Thread(new ThreadStart(oFoo,&Foo::Baz)); 
  
    //  
    // Start the thread  
    //  
    oThread1->Start(); 
  
    // Spin waiting for the started thread to become alive  
    while (!(oThread1->IsAlive)) ; 
  
    // Sleep. Allow oThread1 to do some work.  
    Thread::Sleep(100); 
  
    // Request that oThread1 be stopped  
    oThread1->Stop(); 
  
    // Wait for the oThread1 to finish  
    oThread1->Join(); 
    Console::Out->WriteLine();  
    Console::Out->WriteLine(L"Foo Baz has finished"); 
  
    try  
    {  
         Console::Out->WriteLine(L"Try to restart the Foo Baz thread");  
         oThread1->Start();  
    }  
    catch (ThreadStateException* e)  
    {  
         Console::WriteLine(L"Cannot restart Foo Baz. Expected since stopped threads cannot be restarted.");  
    } 
     
    return 0;  
}

Example: SyncOne

[C++]
#import <mscorlib.dll>

[managed]
class Foo
{
public:

    //
    // The method that will be called when the thread is started
    //
    void Baz()
    {
        int iThreadID = Thread::CurrentThread->GetHashCode();
        int cLoop=100;
        while(cLoop--)
        {
// Obtain the lock
CriticalSection::Enter(this);
Console::Out->Write(L"Hash: ");
Console::Out->Write(iThreadID);
Console::Out->WriteLine(L" Hello ");
 
// Release the lock
CriticalSection::Exit(this);
        }
    }
};

int main() 
{
    Console:: WriteLine(L"Thread Sync One Sample");

    Foo *oFoo = new Foo();

    //
    // Create the 1st thread object, passing in the Foo::Baz method
    // via a ThreadStart delegate
    //
    Thread *Thread1 = new Thread(new ThreadStart(oFoo, &Foo::Baz) );

    //
    // Start the 1st thread
    //
    Thread1->Start();

    //
    // Spin waiting for the started thread to become alive
    //
    while(!Thread1->IsAlive);

    //
    // Create the 2nd thread object, passing in the Foo::Baz method
    // via a ThreadStart delegate, on the same oFoo instance
    //
    Thread *Thread2 = new Thread(new ThreadStart(oFoo, &Foo::Baz) );

    //
    // Start the 2nd thread
    //
    Thread2->Start();

    return 0;
}

Example: SyncTwo

[C++]
#import <mscorlib.dll> 
[managed] class Zap  
{  
}; 
  
[managed] class Foo1  
{  
public:  
    Zap *oZap; 
  
    void Baz()  
    {  
        // Obtain the lock CriticalSection::Enter(oZap); 
        int cLoop=10;  
        while(cLoop--)  
        {  
Console::Out->Write(L" Hello"); 
  
// Release the lock and wait to be Notified  
oZap->Wait(); 
  
// Signal for next thread in wait queue to proceed  
oZap->Notify();  
        } 
  
        // Release the lock  
        CriticalSection::Exit(oZap);  
    }  
}; 
  
[managed] class Foo2  
{  
    public: 
    Zap *oZap; 
  
    void Baz()  
    {  
        // Obtain the lock  
        CriticalSection::Enter(oZap); 
        int cLoop=10;  
        while(cLoop--)  
        {  
Console::Out->WriteLine(L" World");  
  
// Signal for next thread in wait queue to proceed  
oZap->Notify(); 
  
// Release the lock and wait to be Notified  
oZap->Wait();  
        } 
  
        // Release the lock  
        CriticalSection::Exit(oZap);  
    }  
}; 
  
int main()  
{  
    Console::WriteLine(L"Thread Sync Two Sample"); 
    Zap *oZap = new Zap(); 
  
    Foo1 *oFoo1 = new Foo1();  
    oFoo1->oZap = oZap; 
    //  
    // Create the 1st thread object, passing in the Foo::Baz method  
    // via a ThreadStart delegate  
    // 
    Thread *Thread1 = new Thread(new ThreadStart(oFoo1, &Foo1::Baz) ); 
  
    //  
    // Start the 1st thread  
    //  
    Thread1->Start();  
    while(!Thread1->IsAlive); 
  
    Foo2 *oFoo2 = new Foo2();  
    oFoo2->oZap = oZap; 
  
    //  
    // Create the 2nd thread object, passing in the Foo::Baz method  
    // via a ThreadStart delegate, on the same oFoo instance  
    // 
Thread *Thread2 = new Thread(new ThreadStart(oFoo2, &Foo2::Baz) ); 
     
    //  
    // Start the 2nd thread  
    // 
    Thread2->Start(); 
    return 0;  
}

See Also

Thread Members | System.Threading Namespace