Thread C# Tutorial

The lock keyword is used when working with multiple threads.
Its general form is shown here:

lock(obj) {
    // critical section 
    }

using System;
using System.Threading;
class MainClass 
{
  [STAThread]
  static void Main(string[] args)
  {
      int r = 0;
      object t = new object();
    lock ( t )
    { 
      r++;
    }
  }
}