title: D3DImage and SharpDX flickering on slow hardware
body: <p>I am using the SharpDX.WPF project for the WPF abilities, it seems like an easy to understand low-overhead library, compared to the Toolkit that comes with SharpDX (which has the same issue!)</p>
  
  <p>First: I fixed the SharpDX.WPF project for the latest SharpDX using the following: <a href="https://stackoverflow.com/a/19791534/442833">https://stackoverflow.com/a/19791534/442833</a></p>
  
  <p>Then I made the following hacky adjustment to DXElement.cs, a solution that was also done <a href="http://sharpdx.org/forum/4-general/3381-d3dimage-synchronization-techniques" rel="nofollow noreferrer">here</a>:</p>
  
  <pre><code>private Query queryForCompletion;
      public void Render()
      {
          if (Renderer == null || IsInDesignMode)
              return;
  
          var test = Renderer as D3D11;
          if (queryForCompletion == null)
          {
  
              queryForCompletion = new Query(test.Device,
                  new QueryDescription {Type = QueryType.Event, Flags = QueryFlags.None});
          }
  
          Renderer.Render(GetDrawEventArgs());
  
          Surface.Lock();
          test.Device.ImmediateContext.End(queryForCompletion);
          // wait until drawing completes
          Bool completed;
          var counter = 0;
          while (!(test.Device.ImmediateContext.GetData(queryForCompletion, out completed)
                   &amp;&amp; completed))
          {
              Console.WriteLine("Yielding..." + ++counter);
              Thread.Yield();
          }
          //Surface.Invalidate();
          Surface.AddDirtyRect(new Int32Rect(0, 0, Surface.PixelWidth, Surface.PixelHeight));
          Surface.Unlock();
      }
  </code></pre>
  
  <p>Then I render 8000 cubes in a cube pattern...</p>
  
  <blockquote>
    <p>Yielding...</p>
  </blockquote>
  
  <p>gets printed to the console quite often, but the flickering is still there.
  I am assuming that WPF is nice enough to show the image using a different thread before the rendering is done, not sure though...
  This same issue also happens when I use the Toolkit variant of WPF support with SharpDX.</p>
  
  <p>Images to demonstate the issue:</p>
  
  <ul>
  <li><a href="http://puu.sh/dm0Zt/e298de5806.png" rel="nofollow noreferrer">Bad</a></li>
  <li><a href="http://puu.sh/dm11G/64d9fcb251.png" rel="nofollow noreferrer">Better</a></li>
  <li><a href="http://puu.sh/dm12R/5cd0b3031e.png" rel="nofollow noreferrer">Almost</a></li>
  <li><a href="http://puu.sh/dm13x/adb71c8bdc.png" rel="nofollow noreferrer">Intended</a></li>
  </ul>
  
  <p>Note: It randomly switches between these old images, randomly. I am also using really old hardware which makes the flickering much more appearant (GeForce Quadro FX 1700)</p>
  
  <p>A made a repo which contains the exact same source-code as I am using to get this issue:
  <a href="https://github.com/ManIkWeet/FlickeringIssue/" rel="nofollow noreferrer">https://github.com/ManIkWeet/FlickeringIssue/</a></p>