title: Async PDF Document renderer
body: <p>I am currently implementing a asynchronous document renderer by using the windows 8.1 native pdf renderer. My goal is, to to have this renderer render each pdf page asyncrounasly, but it should be possible to change render direction or to overwrite the current render position (these changes are triggered by user interactions). Background for this is, to use this renderer for my own document pager in an Windows Store App. A user should be able to jump to page XX without to wait for it to be rendered in order, but rather jump to it immeadeatly.</p>
  
  <p>So much for the background.</p>
  
  <p>The renderer I implemented so far does work perfecly but I noticed, that it isn't really asynchronous.
  My DocumentRenderer class has a PagRender which does the actuall rendering for one individual page. The method call to render a page is asynchronous, but the rest isn't If another part of my DocumentRenderer takes longer my UI Thread is blocked. For example if I override the render position with User input my renderer has sometimes to ignore XX pages because he rendered them already. This is mostly a issue for huge PDF ducuments (I usesd the PDF Reference with 1300 pages where the renderer sometimes had to ignore hundreds of pages). </p>
  
  <p>Importend to mention is, that I have two events defined: One for ImageRendered and one for RenderingFinished. To use the Renderer I have to subscribe to theese events and handle the rendered images in the UI (the DocumentPager).</p>
  
  <p>Ok here is the code of my render() method: </p>
  
  <pre><code>public async void Render()
  {
      var rendered = ImageRendered;
      if (rendered == null)
      {
          return;
      }
  
      while (!AllPagesRendered())
      {
                 Debug.WriteLine(Tag + "---------------------&gt; Direction: Forward");
              while (RenderPosition &lt; mPageRenderer.getPageCount())
              {                      
                  if (IsPageRendered(RenderPosition))
                  {                          
                      Debug.WriteLine(Tag + "Page " + RenderPosition + " was ignored... already rendered ");
                      RenderPosition++;
                      continue;
                  }                       
                  Uri ImageURL = await mPageRenderer.RenderPage(RenderPosition);
                  string t = ImageURL.ToString();
                  ImageItem image = new ImageItem(new Uri("ms-appx:/" + ImageURL.AbsolutePath), RenderPosition);
                      Debug.WriteLine(Tag + "Render Image at position " + RenderPosition);
                  RenderedImages.Add(image);
                  rendered(this, new RenderArgs(image));                  
                  RenderPosition++;
  
                  if (RestartRendering)
                  {
                           Debug.WriteLine(Tag + "---------------------&gt; Restart Renderer... at position " + NewPosition);
                      RenderPosition = NewPosition;
                      RestartRendering = false;
                  }
  
                  if (ChangeDirection)
                  {
                          Debug.WriteLine(Tag + "---------------------&gt; Change Direction. Restarting renderer...");
                      ChangeDirection = false;
                      break;
                  }
              }
  
              if (!EscapeLoop)
              {
                  if (!AllPagesRendered())
                  {
                         Debug.WriteLine(Tag + "Rendering not finished... RenderPosition back to 0 ");
                      RenderPosition = 0;
                  }
              }
              else
              {
                  EscapeLoop = false;
              }
      }
        Debug.WriteLine(Tag + "---------------&gt; Rendering finished");
      EventHandler handler = RenderingFinished;
      handler(this, new EventArgs());
  }
  </code></pre>
  
  <p>This renderer highly depends on the UI context, so stuff like Render().ConfigureAwait(false); is not an option. I'm also aware that a async void methode is not a good style but I already awaited this render() methode all the way down but it made no difference. </p>
  
  <p>What is the best way to make the entire renderer asynchronous? </p>