title: Parallel Programming: Can't access UI Parallel?
body: <p>I'm trying to create parallel execution of a function in wpf c# which also runs actions on the UI. But when running there is always an exception at methods on UI Controls: The calling thread cannot access this object because a different thread owns it. The exception is always called on the second instance of the loop being run, so it isn't possible to manipulate the UI in two parallel running instances?</p>
  
  <p>Is it possible to acces the UI in parallel?</p>
  
  <p>Code:</p>
  
  <pre><code>do
  {  
      if (listBox_Copy.SelectedIndex &lt; listBox_Copy.Items.Count - 1)
      {
          listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
          listBox_Copy.ScrollIntoView(listBox_Copy.SelectedItem);
      }
      listBox_Copy.Focus();
      huidigitem = listBox_Copy.SelectedItem as ListBoxItem;
      currentitemindex = listBox_Copy.SelectedIndex;
      currentitem = listBox_Copy.ItemContainerGenerator.ContainerFromIndex(currentitemindex) as ListBoxItem;
      itemgrid = FindVisualChild&lt;Grid&gt;(currentitem);
      senderbutton = (Button)sender;
      Button playcues = itemgrid.FindName("Playbutton") as Button;
      cuetrigger = itemgrid.FindName("cuetrigger") as TextBlock;
      Jobs.Add(playcues);
  } while (cuetrigger.Text != "go");
  
  Parallel.ForEach(Jobs, playcues =&gt; { play(playcues, new RoutedEventArgs()); });
  </code></pre>
  
  <p>And then it crashes at the play function</p>
  
  <pre><code>private void play(object sender, System.Windows.RoutedEventArgs e)
  {
      Grid itemgrid = VisualTreeHelperExtensions.FindAncestor&lt;Grid&gt;(playcue);
      ...
  }
  </code></pre>