static void Add(object data) { if (data is AddParams) { Console.WriteLine("ID of thread in Add(): {0}", Thread.CurrentThread.ManagedThreadId); AddParams ap = (AddParams)data; Console.WriteLine("{0} + {1} is {2}", ap.a, ap.b, ap.a + ap.b); } }
static void Add1(object data) { if (data is AddParams) { Console.WriteLine("ID of thread in Add(): {0}", Thread.CurrentThread.ManagedThreadId); AddParams ap = (AddParams)data; Console.WriteLine("{0} + {1} is {2}", ap.a, ap.b, ap.a + ap.b); // Tell other thread we are done. waitHandle.Set(); } }
static void Example3() { Console.WriteLine("****** Adding with Thread objects ******"); Console.WriteLine("ID of thread in Main() : {0}", Thread.CurrentThread.ManagedThreadId); AddParams ap = new AddParams(10, 10); Thread t = new Thread(new ParameterizedThreadStart(Add1)); t.Start(ap); //Wait here until you are notified! waitHandle.WaitOne(); Console.WriteLine("Other Thread is done !"); Console.ReadLine(); }
static void Example2() { Console.WriteLine("****** Adding with Thread objects ******"); Console.WriteLine("ID of thread in Main() : {0}", Thread.CurrentThread.ManagedThreadId); //make AddParams object to pass to the secondary thread. AddParams ap = new AddParams(10, 10); Thread t = new Thread(new ParameterizedThreadStart(Add)); t.Start(ap); //Force a wait to let other thread finish. Thread.Sleep(5); Console.ReadLine(); }