title: Create and update UI elements Async Wpf
body: <p>I'm trying to load several images async into the ui.</p>
  
  <p>When an image is loaded (a bitmap is created form a path), the image should be set as the fill of a rectangle on the window.</p>
  
  <p>When I put the creation of the bitmapimage also in the Dispatcher.Invoke method, the code works. But obviously I want the heavy work (creation of the bitmap) done in the tread and not in the invoke.</p>
  
  <p>I've tried several solutions including backgroundworker but I can't get it to work. Right now I have the following code:</p>
  
  <pre><code>private void Window_ContentRendered(object sender, EventArgs e)
  {
      Thread loadThread = new Thread(new ThreadStart(LoadImagesAsync));
      loadThread.Start();
  }
  
  private void LoadImagesAsync()
  {
      IEnumerable&lt;string&gt; images = System.IO.Directory.GetFiles(IMAGE_FOLDER, "*.jpg").Skip(_PageNumber * NUMBER_OF_IMAGES).Take(NUMBER_OF_IMAGES);
  
      for (int i = 0; i &lt; NUMBER_OF_IMAGES; i++)
      {
          var bitm = new BitmapImage(new Uri(images.ElementAt(i)));                
  
          this.Dispatcher.Invoke(() =&gt;
          {
              Grid grid = (Grid)grd_photoBox.Children[i];
  
              var rectangle = (from e in grid.Children.OfType&lt;Rectangle&gt;()
                                  where e is Rectangle
                                  select e).First();
  
              ImageBrush brush = new ImageBrush(bitm);
  
              rectangle.Fill = brush;
          });
      }
  }
  </code></pre>
  
  <p>I get the following exception:</p>
  
  <pre><code>The calling thread cannot access this object because a different thread owns it.
  </code></pre>
  
  <p>Any clues?</p>