title: How to fix panel flickering when redrawing?
body: <p>I have a panel that I've subclassed to and have set <code>DoubleBuffered</code> true, I constantly need to refresh the drawing but it flickers and have no idea why.</p>
  
  <pre><code>private delegate void MyDelegate();
  
  public void heartBeat()
      {
          while (true)
          {
              if (map.processNubots(rules))
              {
                  if (this.InvokeRequired)
                  {
                      this.Invoke((MyDelegate)delegate
                      {
                          //drawPanel.SuspendLayout();
                          drawPanel.Refresh();
                          displayGrid();
                          //drawPanel.ResumeLayout();
                      });
                  }
                  Thread.Sleep(500);
              }
              else
              {
                  break;
              }
          }
      }
  
      public void displayGrid()
      {
          int i = 0;
          foreach (DictionaryEntry pair in map)
          {
              Monomer current = (Monomer)pair.Value;
              drawMonomers(current.getLocation(), current.getState());
              i++;
          }
      }
  
      public void drawMonomers(Point location, string state)
      {
          ...
  
          SolidBrush sb = new SolidBrush(mycolor);
          SolidBrush sbt = new SolidBrush(Color.Black);
          Graphics g = drawPanel.CreateGraphics();
          Font text = new Font("Arial", scale / 2);
          Pen pen = new Pen(Color.Black, 1);
          pen.Alignment = PenAlignment.Inset;
          g.FillEllipse(sb, offSet + ((location.Y * scale) / 2) + (location.X * scale), offSet + (-location.Y * scale), scale, scale);
          g.DrawEllipse(pen, offSet + ((location.Y * scale) / 2) + (location.X * scale), offSet + (-location.Y * scale), scale, scale);
          g.DrawString(state, text, sbt, (offSet + ((location.Y * scale) / 2) + (location.X * scale)) + scale / 6, (offSet + (-location.Y * scale)) + scale / 6);
  
          sb.Dispose();
          sbt.Dispose();
          pen.Dispose();
      }
  </code></pre>
  
  <p>So after every "computation" and have added something to my imaginary grid, I need to update the panel to show this new item on my grid. I have tried invalidating the panel right before the <code>displayGrid()</code> function but it seems to cause even more flickering.</p>
  
  <p>The <code>heartbeat()</code> function is currently being called on a separate thread.</p>
  
  <p>Here is my new <code>Panel</code> class.</p>
  
  <pre><code>public class Display : Panel
  {
      public Display()
      {
          this.DoubleBuffered = true;
  
      }
  }
  </code></pre>