المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : delegate difficulty



C# Programming
03-25-2013, 03:12 PM
Hello
here is a little difficulty:-
there is a program:- (source:pg 750: C# 4 Complete Reference by Herbert Schildt , McGrawHill )

using System; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> namespace scratchpad3 { class Program { public static void Main() { int[] a = { 1, 2, 3, 4, 5 }; MyThread mt1 = new MyThread("Child #1", a); MyThread mt2 = new MyThread("Child #2", a); mt1.Thrd.Join(); mt2.Thrd.Join(); } } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> class SumArray { int sum; object lockOn = new object(); // a private object to lock on public int SumIt(int[] nums) { lock (lockOn) { // lock the entire method sum = 0; // reset sum for (int i = 0; i < nums.Length; i++) { sum += nums[i]; Console.WriteLine("Running total for " + Thread.CurrentThread.Name + " is " + sum); Console.Read(); Thread.Sleep(10); // allow task-switch } return sum; } } } // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> class MyThread { public Thread Thrd; int[] a; int answer; // Create one SumArray object for all instances of MyThread. static SumArray sa = new SumArray(); // Construct a new thread. public MyThread(string name, int[] nums) { a = nums; Thrd = new Thread(this.Run); // **************************** Thrd.Name = name; Thrd.Start(); // start the thread } // Begin execution of new thread. void Run() { Console.WriteLine(Thrd.Name + " starting."); answer = sa.SumIt(a); Console.WriteLine("Sum for " + Thrd.Name + " is " + answer); Console.WriteLine(Thrd.Name + " terminating."); Console.Read(); } } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> }
the o/p is given as;-

Child #1 starting. Running total for Child #1 is 1 Child #2 starting. Running total for Child #1 is 3 Running total for Child #1 is 6 Running total for Child #1 is 10 Running total for Child #1 is 15 Running total for Child #2 is 1 Sum for Child #1 is 15 Child #1 terminating. Running total for Child #2 is 3 Running total for Child #2 is 6 Running total for Child #2 is 10 Running total for Child #2 is 15 Sum for Child #2 is 15 Child #2 terminating.
My difficulty is at line that is starred
that line shows a delegate but you see in the form: Thrd = new Thread(this.Run);
shouldn't this be: Thrd = new Thread(this.Run()); ---- Is this a paradigm shift or ----????????????????????