title: Automating the InvokeRequired code pattern
body: <p>I have become painfully aware of just how often one needs to write the following code pattern in event-driven GUI code, where</p>
  
  <pre><code>private void DoGUISwitch() {
      // cruisin for a bruisin' through exception city
      object1.Visible = true;
      object2.Visible = false;
  }
  </code></pre>
  
  <p>becomes:</p>
  
  <pre><code>private void DoGUISwitch() {
      if (object1.InvokeRequired) {
          object1.Invoke(new MethodInvoker(() =&gt; { DoGUISwitch(); }));
      } else {
          object1.Visible = true;
          object2.Visible = false;
      }
  }
  </code></pre>
  
  <p>This is an awkward pattern in C#, both to remember, and to type. Has anyone come up with some sort of shortcut or construct that automates this to a degree? It'd be cool if there was a way to attach a function to objects that does this check without having to go through all this extra work, like a <code>object1.InvokeIfNecessary.visible = true</code> type shortcut.</p>
  
  <p>Previous <a href="https://stackoverflow.com/questions/747210/whats-wrong-with-calling-invoke-regardless-of-invokerequired">answers</a> have discussed the impracticality of just calling Invoke() every time, and even then the Invoke() syntax is both inefficient and <em>still</em> awkward to deal with.</p>
  
  <p>So, has anyone figured out any shortcuts?</p>