title: How to update a graph on a panel at 25 FPS in C++/CLI / Windows Forms?
body: <p>My Application is recieving data from external hardware and plots it onto a panel (can be actually any other child of "Control"). The painting currently happens in a "OnPaint" callback. A List is used to store the recently recieved data to allow to redraw the whole graph in OnPaint to get the proportions right if e.g. the window gets resized.</p>
  
  <p>The graph itself is drawn with the <code>e-&gt;Graphics</code> element using lines between two data points.</p>
  
  <p>This is working fine, but when I have new data coming in every 50 ms (= repaint the whole graph), the graph soon begins to flicker. The flickering gets stronger the more data needs to be plotted (when the right side of the control is reached, the data cache gets cleared, so there is a finite max number of data points in the graph).</p>
  
  <p>The main part of my code:</p>
  
  <pre><code>void Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
  {
      Size^ s = m_Control-&gt;Size;
      Bitmap^ bmp = gcnew Bitmap(s-&gt;Width, s-&gt;Height);
      Graphics^ g = Graphics::FromImage(bmp);
  
      for each(double y in m_Data)
      {
          /* ...calculations etc... */
          g-&gt;DrawLine(Pens::Blue, recentX, recentY, currentX, currentY);
      }           
  
      e-&gt;Graphics-&gt;DrawImageUnscaled(bmp, 0, 0);
  }
  </code></pre>
  
  <p>Any suggestion how I can optimize the painting to get rid of the flickering? Thanks in advance!</p>