title: How to use the dispatcher to load multiple images
body: <p>I have the same problem as stated in <a href="https://stackoverflow.com/questions/6627164/silverlight-bitmapimage-from-stream-throws-exception-catastrophic-failure-exc">this question</a>, and the question is answered and the answer seems also to (theoretically) work in my problem. But I can't figure out how to implement <a href="https://stackoverflow.com/questions/6627164/silverlight-bitmapimage-from-stream-throws-exception-catastrophic-failure-exc/6936084#6936084">the answer provided</a>.</p>
  
  <p>It suggest that when loading multiple images I batch them and just do a few and then use the dispatcher to start working on the next few.</p>
  
  <p>I tried to write a function called <code>LoadNextFive(int startIndex)</code> and in the end of the function it just called itself like this: <code>this.Dispatcher.BeginInvoke(() =&gt; { LoadNextFive(startIndex + 5); });</code> but it just didn't seem to work. Am I using the dispatcher wrong or am I implementing the answer wrong?</p>
  
  <p>--EDIT--
  I currently tried to just load 1 image at the time.</p>
  
  <pre><code>public void LoadNextImage()
  {
      if(m_enumerator.MoveNext())
      {
          if (!m_bitmapSources.ContainsKey(m_enumerator.Current))
          {
              ZipEntry imageEntry = m_zip.GetEntry(m_enumerator.Current);
              using (Stream imageStream = m_zip.GetInputStream(imageEntry))
              {
                  BitmapImage source = new BitmapImage();
                  source.SetSource(imageStream);
  
                  m_bitmapSources.Add(m_enumerator.Current, source);
              }
  
              m_dispatcher.BeginInvoke(() =&gt; LoadingTemplate.ChangeText(this, "")); //change loadingtext (provides % in future) and it calls this function again
          }
          else
          {
              m_dispatcher.BeginInvoke(() =&gt; LoadNextImage());
          }
      }else{
          //starts after every image is done loading
          LoadPages(); 
          LoadMonths();
  
          OnLoadComplete(new EventArgs());
      }
  }
  </code></pre>