Threading & The .NET Framework
by Douglas Reilly 


Listing One
Int[] i = new int[4];
i[0]=0;
i[1]=1;
i[2]=2;
i[3]=3;

Listing Two
namespace ThreadingArticle
{
    using System;
    public class Class1
    {
        int[] i;
        public Class1()
        {
           i = new int[4];
        }
        public static int Main(string[] args)
        {
           Class1 c = new Class1();
           c.initializeIt();
           c.readIt()
           return 0;
        }
      public void readIt()
      {
        System.Console.WriteLine("i[3]={0}",i[3]);
      }   
      public void initializeIt()
      {
        i[0]=0;
        i[1]=1;
        i[2]=2;
        i[3]=3;
      }
    }
}


Listing Three
namespace ThreadingArticle
{
   using System;
   using System.Threading;
   public class Class1
   {
      int[] i;
      public Class1()
      {
         i = new int[4];
      }
      public static int Main(string[] args)
      {
            Class1 c = new Class1();
            Thread t = null;
            // Create a thread to run initializeIt()
            t = new 
              Thread(new ThreadStart(c.initializeIt));
            t.Start();
            // Create a thread to run readIt()
            t = new 
              Thread(new ThreadStart(c.readIt));
            t.Start();
            return 0;
        }
        public void readIt()
        {
            System.Console.WriteLine("i[3]={0}",i[3]);
        }
        public void initializeIt()
        {
            i[0]=0;
            i[1]=1;
            i[2]=2;
            i[3]=3;
        }
    }
}


Listing Four
Struct COLOR {
    char red;
    char green;
    char blue;
}


Listing Five
namespace ThreadTest
{
    using System;
    using System.Threading;

    public class ThreadTest
    {
        public System.Threading.AutoResetEvent hEvent;
        // Object to lock() on.
        public Object cs;
        public int threadNum;
        public bool bRunning;
        public ThreadTest()
        {
            bRunning=true;
            hEvent=new System.Threading.AutoResetEvent(false);
            cs=new Object();
            threadNum=0;
     }
     public void WorkerThread() 
     {
        int th=0;
        lock(cs)
        {
            threadNum++;
            th=threadNum;
        }
        while ( bRunning==true )
        {
            if (hEvent.WaitOne(1000,false)==true)
            {
                lock(cs)
                {
                  System.Console.WriteLine("Thread {0} Got Handle, 
                                 and could safely get variable",th);
                }
            }
            else
            {
                Thread.Sleep(1000);
                System.Console.WriteLine("Thread {0} Slept...",th);
            }
        }   // End of while (bRunning==true)
    }   // End of WorkerThread()
    public static int Main(string[] args)
    {
    // We use this to get an object reference to this method. In the 
    // olden days, we would use a static method, because non-static methods
    // have magic, invisible "this" pointers.  
    ThreadTest b=new ThreadTest();
    Thread t=null;
    // If this was doing work we cared about, would have an array of Thread
    // objects, and then use .Join() method to ensure they had exited...
    for (int loop=0 ; loop < 10 ; loop++)
    {
      t = new Thread(new ThreadStart(b.WorkerThread));
      t.Start();
    }
    for (int loop=0;loop<100;loop++)
    {
        Random r = new Random();
        // Just to make it seem like some work was happening...
        Thread.Sleep(r.Next(1,200));
    
        // We lock on cs.  The WorkerThread() will, after returning 
        // successfully from wait, try to acquire lock as well. I will 
        // not try and set event again until thread is done with that 
        // section, presumably, after having read some data.
        lock(b.cs)
        {
            // Muck with data that is shared, and then set event...
            //  should check if this returns true or false...
            b.hEvent.Set();
         }
      }
    b.bRunning=false;
            return 0;
       } // End of Main()
}  // End of Namespace


Listing Six
Imports System.Threading
Module Module1
    Sub Main()
        Dim b As New ThreadTestVB()
        Dim t As Thread
        Dim tloop As Integer
        Dim r As Random
        Dim ts As ThreadStart
        
        t = Nothing
        For tloop = 1 To 10
            ts = New ThreadStart(AddressOf b.WorkerThread)
            t = New Thread(ts)
            t.Start()
        Next
        r = New Random()
        For tloop = 1 To 100
            Thread.Sleep(r.Next(1, 200))
            SyncLock (b.cs)
                b.hEvent.Set()
            End SyncLock
        Next
    End Sub
    Public Class ThreadTestVB
        Public Dim hEvent As AutoResetEvent
        Public Dim cs As Object
        Dim threadNum As Integer
        Dim bRunning As Boolean
        
        Public Sub New()
            bRunning = True
            hEvent = New AutoResetEvent(False)
            cs = New Object()
            threadNum = 0
        End Sub
        
        Public Sub WorkerThread()
            Dim th As Integer
            SyncLock (cs)
                threadNum = threadNum + 1
                th = threadNum
            End SyncLock
            While bRunning = True
                If hEvent.WaitOne(1000, False) = True Then
                    SyncLock (cs)
                        System.Console.WriteLine( _
        "Thread {0} Got Handle and could safely get variable", _th)
                    End SyncLock
                Else
                    Thread.Sleep(1000)
                    system.Console.WriteLine( _"Thread {0} Slept", th)
                End If
            End While
        End Sub
    End Class
End Module


Listing Seven
for ( int loop=0 ;loop<10 ; loop++)
{
    t = new Thread(new ThreadStart(b.WorkerThread));
    t.Start();
}

Listing Eight
System.Threading.Monitor.Enter(this);
try
{
    // code here
}
finally
{
    System.Threading.Monitor.Exit(this);
}


Listing Nine
lock()
{
    // code here
}

Listing Ten
if (hEvent.WaitOne(1000,false)==true)
{
lock(cs)
    {
        System.Console.WriteLine(
    "Thread {0} Got Handle, and could safely get variable", th);
    }
}
else
{
    Thread.Sleep(1000);
    System.Console.WriteLine("Thread {0} Slept...",th);
}




5

