title: Lock code section to one entrance at a time
body: <p>I am trying to restrict access to an singletone object so only one thread
  use it at time, Furthermore, I want to prevent from the same thread accessing twice 
  to the restricted code.</p>
  
  <p>I tried the Lock method and i found out that its dosn't lock the thread that locked her, but only other threads..</p>
  
  <p>as below: </p>
  
  <pre><code>public sealed class Singleton
  {
      private static readonly Singleton instance    = new Singleton();
  
      static Singleton()
      {
      }
  
      private Singleton()
      {
      }
  
      public static Singleton Instance
      { 
          get
          {
              return instance;
          }
      }
  }
  
  public class SomeWorker
  {
      private readonly Timer _doWorkTimer = new Timer(20);
  
      public SomeWorker()
      {
          InitiateTimer();
      }
  
      private void InitiateTimer()
      { 
          _doWorkTimer .Elapsed += DoWorkElapse;
          _doWorkTimer .Enabled = true;
      }
  
      private void DoWorkElapse(object source, ElapsedEventArgs e)
      { 
          DoSomeWork();
      }
  
      private void DoSomeWork()
      {
          // I know that lock on string is wrong!
          // Its just for the example only I
          // Its just to make sure all the program is use the same lock..
          lock ("ConnectionLock")
          { 
               Console.WriteLine("Lock");
  
               var inst = Singletone.Instance;
  
               // Do Some Work on "inst" ...
  
               Console.WriteLine("Unlock");
          }
      }
  }
  </code></pre>
  
  <p>The result in the console for example is:</p>
  
  <p>.
  .
  .</p>
  
  <p>Lock</p>
  
  <p>Unlock</p>
  
  <p><strong>Lock</strong></p>
  
  <p><strong>Lock</strong> </p>
  
  <p>Unlock</p>
  
  <p>.
  .
  .</p>
  
  <p>As we can see, 2 Lock comments shows one after another</p>
  
  <p>So its mean that the "DoSomeWork()" accessed twice by the timer thread.</p>
  
  <p>Anyone have any idea how to make this lock work?</p>
  
  <p>Other Sync methods maby?</p>
  
  <p>thanx.</p>