Mega Code Archive

 
Categories / C# by API / System Threading
 

Thread Join()

using System; using System.Threading;     class Sum {     public Sum(int op1, int op2) {         Console.WriteLine("[Sum.Sum] Instantiated with values of {0} and {1}", op1, op2);         this.op1 = op1;         this.op2 = op2;     }     int op1;     int op2;     int result;     public int Result{ get { return result; } }         public void Add()     {         Thread.Sleep(5000);         result = op1 + op2;     } }     class ThreadData {     static void Main() {         Sum sum = new Sum(6, 42);             Thread thread = new Thread(new ThreadStart(sum.Add));         thread.Start();             for (int i = 0; i < 10; i++) {             Thread.Sleep(200);             Console.Write(".");         }         thread.Join();             Console.WriteLine("[Main] The result is {0}", sum.Result);         Console.ReadLine();     } }