Thread.Join() : Thread « System.Threading « C# / C Sharp by API






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();
    }
}

 

   
    
  








Related examples in the same category

1.new Thread()
2.Thread.Abort()
3.Thread.AllocateDataSlot
4.Thread.AllocateNamedDataSlot
5.Thread.CurrentContext
6.Thread.CurrentCulture
7.Thread.CurrentThread
8.Thread.GetData
9.Thread.GetHashCode()
10.Thread.Interrupt()
11.Thread.IsAlive
12.Thread.IsBackground
13.Thread.Priority
14.Thread.ResetAbort()
15.Thread.SetData
16.Thread.Sleep
17.Thread.Start
18.Thread.ThreadState