title: Efficiently plot a real-time waveform
body: <p>I'm plotting a waveform in WPF using the following XAML:</p>
  
  <pre><code>&lt;Canvas&gt;
      &lt;Path Stroke="Black" StrokeThickness="3"&gt;
          &lt;Path.Data&gt;
              &lt;PathGeometry&gt;
                  &lt;PathFigure x:Name="figure" IsClosed="False"&gt;
                      &lt;PathFigure.Segments&gt;
                          &lt;PolyLineSegment x:Name="polyline"/&gt;
                      &lt;/PathFigure.Segments&gt;
                  &lt;/PathFigure&gt;
              &lt;/PathGeometry&gt;
          &lt;/Path.Data&gt;
      &lt;/Path&gt;
  &lt;/Canvas&gt;
  </code></pre>
  
  <p>I have a simple timer runnign every 32 ms that does nothing more than to add a random point to the line:</p>
  
  <pre><code>private void dispatcherTimer_Tick(object sender, EventArgs e)
  {
       xPos++;
       polyline.Points.Add(new Point(xPos, yPos + ((int)(rnd.NextDouble() * 10) - 5)));
  }
  </code></pre>
  
  <p>This works fine, but as soon as I do the same thing with more than 10 lines, the CPU usage goed through the roof. </p>
  
  <p>Upon inspecting the application with the WPF Profiler (Perforator), I can see that WPF marks the entire PathGeometry as a 'dirty region', instead of just updating the changes to the screen. So instead of noticing only a single pixel was changed, it redraws almost the complete form on each update.</p>
  
  <p>I don't know if this is the reason for the excessive CPU usage, but can someone tell me how to optimize this (very simple) program, or what I'm doing wrong?</p>