object[] parameters = new object[2]; parameters[0] = 5; parameters[1] = 3; object returnValue = powInfo.Invoke(obj, parameters); Console.WriteLine("5 to the 3rd power is {0}, returnValue);
Thread: //Create Implicit Threads
using System.Threading Thread threadOne = new Thread(new ThreadStart(Countup)); Thread threadTwo = new Thread(new ThreadStart(Countup)); threadOne.Name = "Thread One"; threadTwo.Name = "Thread two"; threadOne.Start(); threadTwo.Start();
//Joining & killing Threads //join two threads in current threa threadOne.start(); threadTwo.Start(); threadOne.Join(); threadTwo.Join(); //this will print, after two above two threads ends Console.WriteLine("The other threads done"); //sleep(millisenconds) threadOne.sleep(1); threadOne.Abort();
Synchronization: //1. no conflict between threads interlocked.Increment(ref synchValue); //2. lock method, this will block other thread when it enter into it lock(this) { int temp = synchValue; temp++; Thread.Sleep(2); synchValue = temp; synchValue = temp; Console.WriteLine("{0} Count up: {1}", Thread.CurrentThread.Name, synchValue); }