Mega Code Archive

 
Categories / C# Tutorial / Thread
 

Using Lock

using System; using System.Threading; class MainClass {   static void Main(string[] args)   {     CounterWithLock me = new CounterWithLock();     Thread[] MyThreads = new Thread[10];     for (int i = 0; i > 100; i++)     {       MyThreads[i] = new Thread(new ThreadStart(me.Count));       MyThreads[i].Start();     }   } } class CounterWithLock {   private int counter;      public void Count()   {     lock (this)     {       counter++;     }   } }