title: 2 threads entering a locked block of code at the same time
body: <p>I am attempting to allow only 1 thread at a time to enter a certain block of code thusly:</p>
  
  <pre><code>private static object lockObject = new object();
  
  protected override void WndProc(ref Message message) {
      if (id != null) {
          id.ProcessMessage(message);
  
          lock(lockObject) {
              if (id.ScanCode.Length &gt; 4) {
                  ...
              }
          }
      }
  
      base.WndProc(ref message);
  }
  </code></pre>
  
  <p>Sometimes 2 threads get in there and both execute the code. I have confirmed this in the VS debugger. Sometimes only 1 thread gets in, which is good, but at least half the time, 2 threads get in -- never more than 2 it seems.</p>
  
  <p>Inside that if-block I'm doing this: </p>
  
  <pre><code>id.ScanCode = string.Empty;
  return;
  </code></pre>
  
  <p>To me it seems this should prevent the 2nd thread from entering, if it did get blocked. But maybe I'm doing this wrong. What's the simplest way to prevent the 2nd thread from getting in there? Could they be entering simultaneously?</p>