title: WriteableBitmap.Lock() Performance Issue
body: <p>I have an application where performance-sensitive drawings occur using a <code>WriteableBitmap</code>. An event is called with <code>CompositionTarget.Rendering</code> to actually update the back buffer of the <code>WriteableBitmap</code>.  From the MSDN documentation, that means the event is fired once per frame, right before the control is rendered.  </p>
  
  <p>The issue that I am having is that the WriteableBitmap's <code>Lock()</code> function takes an extremely long time, especially at larger bitmap sizes.  I have previously read that <code>AddDirtyRegion()</code> has a bug that causes the entire bitmap to invalidate, leading to poor performance.  However, that doesn't seem to be the case here.  From a good bit of low-level checking, it seems that <code>Lock()</code> opens the bitmap's backbuffer for writing on the render thread, which means every time my event handler is called, it has to thread block until the render thread is ready for it.  This leads to a noticeable lag when updating the graphics of the bitmap.  </p>
  
  <p>I have already tried adding a timeout to the event handler, using <code>TryLock()</code>, so that it won't block for such a long time and cause the performance degradation.  This, however, causes a similar effect in that it appears to lag, because larger numbers of bitmap updates get lumped together. </p>
  
  <p>Here is the relevant code from the event handler to show what exactly I am doing.  The <code>UpdatePixels()</code> function was written to avoid using the potentially bugged <code>AddDirtyRect()</code>:</p>
  
  <pre><code>void updateBitmap(object sender, EventArgs e)
  {
      if (!form.ResizingWindow)
      {
          // Lock and unlock are important... Make sure to keep them outside of the loop for performance reasons.
          if (canvasUpdates.Count &gt; 0)
          {
              //bool locked = scaledDrawingMap.TryLock(bitmapLockDuration);
              scaledDrawingMap.Lock();
              //if (locked)
              //{
              unsafe
              {
                  int* pixData = (int*)scaledDrawingMap.BackBuffer;
                  foreach (Int32Rect i in canvasUpdates)
                  {
                      // The graphics object isn't directly shown, so this isn't actually necessary.  We do a sort of manual copy from the drawingMap, which acts similarly
                      //    to a back buffer. 
                      Int32Rect temp = GetValidDirtyRegion(i);
                      UpdatePixels(temp, pixData);
                  }
                  scaledDrawingMap.Unlock();
                  canvasUpdates.Clear();
              }
              //}
          }
      }
  }
  
  private unsafe void UpdatePixels(Int32Rect temp, int* pixData)
  {
      //int* pixData = (int*)scaledDrawingMap.BackBuffer;
       // Directly copy the backbuffer into a new buffer, to use WritePixels().
      var stride = temp.Width * scaledDrawingMap.Format.BitsPerPixel / 8;
      int[] relevantPixData = new int[stride  * temp.Height];
      int srcIdx = 0;
      int pWidth = scaledDrawingMap.PixelWidth;
      int yLess = temp.Y + temp.Height;
      int xLess = temp.X + temp.Width;
      for (int y = temp.Y; y &lt; yLess; y++)
      {
          for (int x = temp.X; x &lt; xLess; x++)
          {
              relevantPixData[srcIdx++] = pixData[y * pWidth + x];
          }
      }
      scaledDrawingMap.WritePixels(temp, relevantPixData, stride, 0);
  }
  </code></pre>
  
  <p>I can't seem to figure out how to avoid the issue of thread blocking with the WriteableBitmap, and I can't see any obvious faults in the code I have written. Any help or pointers would be much appreciated.</p>