Use anonymous delegate as the worker method to create Thread : Thread Creation : Thread C# Examples


C# Examples » Thread » Thread Creation »

 

Use anonymous delegate as the worker method to create Thread









    
using  System;
using  System.Collections.Generic;
using  System.Diagnostics;
using  System.IO;
using  System.Reflection;
using  System.Runtime;
using  System.Runtime.CompilerServices;
using  System.Security;
using  System.Text;

public  class  MainClass
{
        public  static  void  Main()
        {
                int  threadCount  =  5;
                Thread[]  threads  =  new  Thread[threadCount];

                for  (int  i  =  0;  i  <  threadCount;  i++)
                {
                        int  idx  =  i;
                        threads[i]  =  new  Thread(delegate()  {  Console.WriteLine("Worker  {0}",  idx);  });
                }

                Array.ForEach(threads,  delegate(Thread  t)  {  t.Start();  });
        }
}
    
   
  
   



Output

Worker 0
Worker 1
Worker 2
Worker 3
Worker 4


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Thread
» Thread Creation