title: Multiple Parallel Threads In Custom Control
body: <p>I am inside wpf and I have a custom control</p>
  
  <pre><code>public class MyListControl : ListBox
  {
     ...
  </code></pre>
  
  <p>Now I would like to run through <code>Items</code> but using 4 different threads. One thread should take first part of the list, second thread should apply computations on second part and so on...</p>
  
  <p>Take a look at this pseudo code:</p>
  
  <pre><code>var originalSource = this.ItemsSource.Cast&lt;object&gt;().ToList();
  int part = originalSource.Count / 4;
  
  Task.Factory.StartNew(..
      DoSomething(originalSource.Take(part));
  
  Task.Factory.StartNew(..
      DoSomething(originalSource.Skipt(part).Take(part), ...);
  
  Task.Factory.StartNew(..
      DoSomething(originalSource.Skip(part * 2).Take(part), ... );
  
  Task.Factory.StartNew(..
      DoSomething(originalSource.Skip(part * 3).Take(part), ...); ...
  </code></pre>
  
  <p>To sum up the list gets divided in 4 pieces and 4 different threads shall speed up the computations.</p>
  
  <p>Now the question is how do I do this?</p>
  
  <p>I cannot do this in ViewModel, it has to be inside the custom control!</p>
  
  <p>Task.Factory.StartNew seems to be wrong tool for this since I get exceptions like:</p>
  
  <p>A first chance exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll</p>
  
  <p>What do you guys suggest me to do?</p>
  
  <p>I tried Dispatcher.BeginInvoke but they are being invoked one after another and not parallel.</p>
  
  <p>I need a parallel solution for this. Thanks in advance.</p>