Using Lock : Lock : Thread C# Examples


C# Examples » Thread » Lock »

 

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++;
        }

    }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Thread
» Lock