title: Find out which winforms controls are accessed from a background thread
body: <p>We have built a huge winforms project, already in progress for multiple years.</p>
  
  <p>Sometimes, our users get an exception which looks like <a href="https://www.devexpress.com/Support/Center/Question/Details/B192711/calcdefaulttextsize-null-reference-exception-the-object-is-currently-in-use-elsewhere" rel="noreferrer">this one</a>.</p>
  
  <p>The resolution of this problem seems to be: </p>
  
  <blockquote>
    <p>don't acces UI components from a background thread</p>
  </blockquote>
  
  <p>.</p>
  
  <p>But since our project is a very big project with a lot of different threads, we don't succeed in finding all these.</p>
  
  <p>Is there a way to check (with some tool or debugging option) which components are called from a background thread?</p>
  
  <p><strong>To clarify:</strong></p>
  
  <p>I created a sample winforms project with a single <code>Form</code>, containing two <code>Button</code></p>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          button1.Text = "Clicked!";
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
  
          Task.Run(() =&gt;
          {
              button2.BackColor = Color.Red; //this does not throw an exception
              //button2.Text = "Clicked"; //this throws an exception when uncommented
          });
      }
  }
  </code></pre>
  
  <p>The background color of <code>button2</code> is set to red when the button is clicked. This happens in a background thread (which is considered bad behavior). However, it doesn't (immediately) throw an exception. I would like a way to detect this as 'bad behavior'. Preferably by scanning my code, but if it's only possible by debugging, (so pausing as soon as a UI component is accessed from a background thread) it's also fine.</p>