Part 89 ParameterizedThreadStart delegate
Use ParameterizedThreadStart delegate to pass data to the thread function
class Program { static void Main(string[] args) { Console.WriteLine("input a target number:"); object target = Console.ReadLine(); ParameterizedThreadStart pt = new ParameterizedThreadStart(Number.Print);//really need to write that?No,you can just pass the function into Thread class. Thread t = new Thread(pt);//new Thread(Number.Print); t.Start(target); Console.ReadLine(); } } public class Number { public static void Print(object target) { int number = 0; if (int.TryParse(target.ToString(), out number)) { for (int i = 0; i < number; i++) { Console.WriteLine(i); } } } } View CodeHow we are not explictly creating an instance of ParameterizedThreadStart delegate.Then how is it working?
It's working because, the compiler implicitly converts new Thread(Number.Print); to new Thread(new ParameterizedThreadStart(Number.Print));
When to use ParameterizedThreadStart over ThreadStart delegate?
Use ParameterizedThreadStart delegate if you have some data to pass to the Thread function, otherwise just use ThreadStart delegate.
Please note: Using parameterizedThreadStart delegate and Thread.Start(Object) method to pass data to the Thread function is not type safe as they operate on object datatype and any type of data can be passed.
If you try to change the data type of the target parameter of Print() function from object to int, a compiler error will be raised as the signature of Print() function does not match with the signature of ParameterizedThreadStart delegate.
Part 90 Passing data to the Thread function in a type safe manner(in a manner..在某種程度上)
To pass data to the Thread function in a type safe manner, encapsulate the thread function and the data it needs in a helper class and use the ThreadStart delegate to execute the thread function.
class Program { static void Main(string[] args) { Console.WriteLine("input a target number:"); int target = Convert.ToInt32(Console.ReadLine()); Number number = new Number(target); Thread t = new Thread(number.Print); t.Start(); Console.ReadLine(); } } public class Number { int _target; public Number(int target) { this._target = target; } public void Print() { int number = 0; for (int i = 0; i < _target; i++) { Console.WriteLine(i); } } } View CodePart 91 Retrieving data from Thread function using callback method
Callback method to get data from thread
Main thread retrieves(檢索) the target number from the user.
Main thread creates a child thread and pass the target number to the child thread.
The child thread computes the sum of numbers and then returns the sum to the Main thread using callback function.
The callback method prints the sum of numbers.
Step 1: Create a callback delegate. The actual callback method signature should match with the signature of this delegate.
public delegate void SumOfNumberCallback(int sumOfNumber);
Step 2:Create a helper class to compute the sum of numbers and to call the callback method.
public class Number { int _target; SumOfNumberCallback _callBackMethod; public Number(int target, SumOfNumberCallback callBackMethod) { this._target = target; this._callBackMethod = callBackMethod; } public void PrintSumOfNumber() { int sum = 0; for (int i = 1; i <= _target; i++) { sum = sum + i; } if(_callBackMethod!=null) { _callBackMethod(sum); } } } View CodeStep 3:This class consumes the Number class create in Step 2
class Program { public static void Main(string[] args) { Console.WriteLine("input a target number:"); int target = Convert.ToInt32(Console.ReadLine()); SumOfNumberCallback callBack = new SumOfNumberCallback(PrintSum); Number number = new Number(target,callBack); Thread t = new Thread(number.PrintSumOfNumber); t.Start(); Console.ReadLine(); } public static void PrintSum(int sum) { Console.WriteLine("sum of number =" + sum); } } View Code