Pass an Argument to a Thread


Prior to C# 2.0, it was not possible to pass an argument to a thread when the thread was started because the method that serves as the entry point to a thread could not have a parameter. If information needed to be passed to a thread, various work-arounds (such as using a shared variable) were required. However, with the release of C# 2.0, this situation has changed. It is now possible to pass an argument to a thread. To do so, you must use new forms of Start( ), the Thread constructor, and the entry point method.

An argument is passed to a thread through this version of Start( ), which was added by C# 2.0. This version has a parameter and is shown here:

 public void Start(object arg)

The object passed to arg is automatically passed to the thread’s entry point method. Thus, to pass an argument to a thread, you pass it to Start( ).

To make use of the parameterized version of Start( ), you must use the following form of the Thread constructor (added by C# 2.0):

 public Thread(ParameterizedThreadStart entryPoint)

Here, entryPoint is the name of the method that will be called to begin execution of the thread. Notice that in this version, the type of entryPoint is ParameterizedThreadStart rather than ThreadStart, as used by the preceding examples. ParameterizedThreadStart is a delegate added by C# 2.0 and is declared as shown here:

 public delegate void ParameterizedThreadStart(object arg)

As you can see, this delegate takes an argument of type object. Therefore, to use this form of the Thread constructor, the thread entry point method must have an object parameter.

Here is an example that demonstrates the passing of an argument to a thread.

 // Passing an argument to the thread method. using System; using System.Threading; class MyThread {   public int count;   public Thread thrd;   // Notice that MyThread is also passed an int value.   public MyThread(string name, int num) {     count = 0;     // Explicitly invoke ParameterizedThreadStart constructor     // for the sake of illustration.     thrd = new Thread(new ParameterizedThreadStart(this.run));     thrd.Name = name;      // Here, Start() is passed num as an argument.     thrd.Start(num);   }   // Notice that this version of run() has   // a parameter of type object.   void run(object num) {     Console.WriteLine(thrd.Name +                       " starting with count of " + num);     do {       Thread.Sleep(500);       Console.WriteLine("In " + thrd.Name +                         ", count is " + count);       count++;     } while(count < (int) num);     Console.WriteLine(thrd.Name + " terminating.");   } } class PassArgDemo {   public static void Main() {     // Notice that the iteration count is passed     // to these two MyThread objects.     MyThread mt = new MyThread("Child #1", 5);     MyThread mt2 = new MyThread("Child #1", 3);     do {       Thread.Sleep(100);     } while (mt.thrd.IsAlive | mt2.thrd.IsAlive);     Console.WriteLine("Main thread ending.");   } }

The output is shown here:

 Child #1 starting with count of 5 Child #1 starting with count of 3 In Child #1, count is 0 In Child #1, count is 0 In Child #1, count is 1 In Child #1, count is 1 In Child #1, count is 2 In Child #1, count is 2 Child #1 terminating. In Child #1, count is 3 In Child #1, count is 4 Child #1 terminating. Main thread ending.

As the output shows, the first thread iterates five times and the second thread iterates three times. The iteration count is specified in the MyThread constructor and then passed to the thread entry method run( ) through the use of the ParameterizedThreadStart version of Start( ).

For the sake of illustration, the thread is created by explicitly calling the ParameterizedThreadStart delegate constructor in this line:

 thrd = new Thread(new ParameterizedThreadStart(this.run));

As explained earlier, because of the new method group conversion feature, the line can be more efficiently written like this:

 thrd = new Thread(this.run);




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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