title: What happens to a lock during an Invoke/BeginInvoke? (event dispatching)
body: <blockquote>
    <p>Just to put the questions upfront (please no comments about the bad architecture or how to revise - suppose this is what it is):</p>
    
    <ol>
    <li>How does the "lock" statement apply when using Invoke/BeginInvoke </li>
    <li>Could the following code result in a deadlock?</li>
    </ol>
  </blockquote>
  
  <p>Suppose I have the following BindingList that I need to update on the GUI Thread:</p>
  
  <pre><code>var AllItems = new BindingList&lt;Item&gt;();
  </code></pre>
  
  <p>I want to make sure that all updates to it are synchronized.
  Suppose I have the following subroutine to do some calculations and then insert a new entry into the BindingList:</p>
  
  <pre><code>private void MyFunc() {
    lock(locker) {
      ... //do some calculations with AllItems
  
      AddToArray(new Item(pos.ItemNo));
  
      ... //update some other structures with the contents of AllItems
      }
  }
  </code></pre>
  
  <p>And AddToArray looks like:</p>
  
  <pre><code>private void AddToArray (Item pitem)
  {
     DoInGuiThread(() =&gt;
       {
         lock (locker)
           {
             AllItems.Add(pitem);
           }
        });
   }
  </code></pre>
  
  <p>And DoInGuiThread looks like:</p>
  
  <pre><code> private void DoInGuiThread(Action action) {
     if(InvokeRequired) {
         BeginInvoke(action);
      } else {
         action.Invoke();
      }
   }
  </code></pre>