Passing Parameters to Threads


The code in Listing 15.2 was relatively simple. A significant missing item was to pass data from the main thread to the second thread. In C# 1.0, this was cumbersome because the THRead constructor could handle only the System.Threading.ThreadStart delegate type, and it did not take parameters. However, C# 2.0 includes an additional thread constructor, one that takes the System.Threading.ParameterizedThreadStart delegate type. This delegate takes a parameter of type object, making it possible to pass multiple data elements to the thread by wrapping them in a custom class or a collection. Listing 15.3 demonstrates a simple character-passing example, and Output 15.2 shows the results.

Listing 15.3. Using ParameterizedThreadStart to Pass Data

 using System; using System.Threading; class PassingParametersUsingParameterizedThreadStart {    public const int Repetitions = 1000;    public static void Main()    {          //DoWork() now matches the signature of                                      // ParameterizedThreadStart rather than ThreadStart.                         Thread thread = new Thread(DoWork);                                          thread.Start('.');                                                          for  (int count = 0; count < Repetitions; count++)        {             Console.Write('-');        }        thread.Join();     }     public static void DoWork(object state)                                      {        for (int count = 0; count < Repetitions; count++)        {            Console.Write(state);        }    } }

In cases where the new thread requires multiple pieces of data, you must declare a type to hold this data for the new thread. You pass this data as an instance of the new type (see Listing 15.4).

Listing 15.4. Using ParameterizedThreadStart to Pass Multiple Data Elements

using System; using System.Threading; struct DoWorkData {   public DoWorkData(int repetitions, char character)   {        _Repetitions = repetitions;        _Character = character;   }   public int Repetitions   {        get { return _Repetitions; }   }   private int _Repetitions;   public char Character   {        get { return _Character; }   }        private char _Character;   }        class PassingMultipleParametersUsingParameterizedThreadStart   {        public static void Main()         {             const int repetitions = 1000;             // DoWork() now matches the signature of             // ParameterizedThreadStart rather than ThreadStart.             Thread thread = new Thread(DoWork);             thread.Start( new DoWorkData(repetitions, '.') );        for (int count = 0; count < repetitions; count++)        {             Console.Write('-');        }        thread.Join();      }      public static void DoWork(object state)      {        DoWorkData data = (DoWorkData)state;        for (int count = 0; count < data.Repetitions; count++)        {             Console.Write(data.Character);        }     } }

Output 15.2.

................................----------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- --------------------------------------------------------------...........  ......................................................................... ......................................................................... ......................................................................... ......................................................................... ......................................................................... ...................------------------------------------------------------ ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- -------------------------------------------------........................  ......................................................................... ......................................................................... ......................................................................... ......................................................................... ......................................................................... ......------------------------------------------------------------------- ------------------------------------------------------------------------- ----------------------------------------------------------------------...  ......................................................................... ......................................................................... .............................

The results of Listing 15.4 appear in Output 15.3. An alternative to declaring a type is to pass an array or collection.

Output 15.3.

................................----------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- --------------------------------------------------------------...........  ......................................................................... ......................................................................... ......................................................................... ......................................................................... ......................................................................... ...................------------------------------------------------------ ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- ------------------------------------------------------------------------- ----------------------------------------------...........................  ......................................................................... ......................................................................... ......................................................................... ......................................................................... ......................................................................... ...---------------------------------------------------------------------- ------------------------------------------------------------------------- ----------------------------------------------------------------------...  ......................................................................... ......................................................................... .............................

C# 1.0 does not support the ParameterizedThreadStart delegate. An alternative method to pass data in C# 1.0 is to store it in a location accessible from both threads. The problem is associating a thread with its own data. It is sometimes necessary to define a class that contains the thread delegate, instantiate this new class, set the shared data, and call the thread to start the delegate associated with the new class method. For many scenarios, this is overkill. The THReadPool class, described next, is a simpler mechanism.




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net