title: How to detect a control being invalidated?
body: <p>I'm implementing producer/consumer problem. the code looks like this:  </p>
  
  <pre><code>void producer()
  {
    // produce item
    // update some control in form
  }
  
  void consumer()
  {
    // consume item
    // update some control in form
  }
  </code></pre>
  
  <p>producer and consumer methods are executed in different threads from the one that created my form, so I can't update controls in form. I tried following code:  </p>
  
  <pre><code>void producer()
  {
    // produce item
    // put the work to be done in a queue
    this.Invalidate();
  }
  
  void consumer()
  {
    // consume item
    // put the work to be done in a queue
    this.Invalidate();
  }
  </code></pre>
  
  <p>So now I have to detect if the form has been invalidated. I looked in Form's event list, and the best thing I could find was paint event. I put the code that got the job done, and it works fine. The problem is I somehow doubt I've done this the right way although it works. I think paint is not the right place to do the job, as what I'm doing it not just painting. I was wondering if there's a better way to do this.  </p>
  
  <p>Edit -- Snippet for Invalidated event handler not working  </p>
  
  <pre><code>public Form1()
  {
    InitializeComponent();
    this.Invalidated += InvalidateEventHandler;
  }
  void producer(object o)
  {
    // produce
    // put work in queue
    this.Invalidate();
  }
  public void InvalidateEventHandler(object sender, InvalidateEventArgs e)
  {
    // Do Stuff to form -- Where exception raises
  }
  </code></pre>