title: Displaying images with a high frame rate
body: <p>here's the problem: I have a custom hardware device and I have to grab images from it in C#/WPF and display them in a window, all with 120+ FPS.</p>
  
  <p>The problem is that there is no event to indicate the images are ready, but I have to constantly poll the device and check whether there are any new images and then download them.</p>
  
  <p>There are apparently a handful of ways to do it, but I haven't been able to find the right one yet.</p>
  
  <p>Here's what I tried:</p>
  
  <ul>
  <li><p>A simple timer (or DispatcherTimer) - works great for slower frame rates but I can't get it past let's say, 60 FPS.</p></li>
  <li><p>An single threaded infinite loop - quite fast but I have to put the DoEvents/it's WPF equivalent in the loop in order for window to be redrawn; this has some other unwanted (strange) consequences such as key press events from some controls not being fired etc..</p></li>
  <li><p>Doing polling/downloading in another thread and displaying in UI thread, something like this:</p>
  
  <pre><code> new Thread(() =&gt;
          {
              while (StillCapturing)
              {
                  if (Camera.CheckForAndDownloadImage(CameraInstance))
                  {
                      this.Dispatcher.Invoke((Action)this.DisplayImage);
                  }
              }
          }).Start();
  </code></pre>
  
  <p>Well, this works relatively well, but puts quite a load on a CPU and of course completely kills the machine if it doesn't have more than one CPU/core, which is unacceptable. Also, I there is a large number of thread contentions this way.</p></li>
  </ul>
  
  <p>The question is obvious - are there any better alternatives, or is one of these the way to go in this case?</p>
  
  <p><strong>Update:</strong><br>
  I somehow forgot to mention that (well, forgot to think about it while writing this question), but of course I don't need <em>all</em> frames to be displayed, however I still need to capture all of them so they can be saved to a hard drive. </p>
  
  <p><strong>Update2:</strong>
  I found out that the DispatcherTimer method is <em>slow</em> not because it can't process everything fast enough, but because the DispatcherTimer waits for the next vertical sync before firing the tick event; which is actually good in my case, because in the tick event I can save all pending images to a memory buffer (used for saving images to disk) and display just the last one.</p>
  
  <p>As for the old computers being completely "killed" by capturing, it appears that WPF falls back to software rendering which is very slow. There's probably nothing I can do about.</p>
  
  <p>Thanks for all the answers.</p>