private void RenderThreadFunc()
 {
     lock (this)
     {
         _activeRequest = null;
         if (_pendingRequest != null)
         {
             _activeRequest  = _pendingRequest;
             _pendingRequest = null;
         }
     }
     if (_activeRequest != null)
     {
         try
         {
             // Return null if no update required
             BitmapSource result = Dispatcher.HasShutdownStarted ? null : (BitmapSource)RenderFrame(_activeRequest.Visible, _activeRequest.Output, _activeRequest);
             if (result != null)
             {
                 Dispatcher.BeginInvoke(
                     new RenderCompletionHandler(OnRenderCompleted),
                     new RenderResult(_activeRequest, result));
             }
         }
         catch (Exception exc)
         {
             Trace.WriteLine(string.Format("RenderRequest {0} failed: {1}", _activeRequest.RequestID, exc.Message));
         }
     }
 }
        protected virtual void OnRenderCompleted(RenderResult result)
        {
            if (result.IsSuccess)
            {
                _completedRequest  = result.Request;
                _completedBitmap   = result.Bitmap;
                _bitmapInvalidated = false;

                InvalidateVisual();
                BackgroundRenderer.RaiseRenderingFinished(this);
            }
        }
        private void CreateRenderTask(DataRect visible, Rect output)
        {
            lock (this)
            {
                _bitmapInvalidated = true;

                if (_activeRequest != null)
                {
                    _activeRequest.Cancel();
                }
                _pendingRequest = new RenderRequest(_nextRequestId++, visible, output);
                _renderAction.InvokeAction(RenderThreadFunc);
            }
        }
 /// <summary>Constructs successul rendering result</summary>
 /// <param name="request">Source request</param>
 /// <param name="result">Rendered bitmap</param>
 public RenderResult(RenderRequest request, BitmapSource result)
 {
     _request = request;
     _source  = result;
 }
 protected abstract BitmapSource RenderFrame(DataRect visible, Rect output, RenderRequest renderRequest);