title: How to query what thread is running
body: <p>In my program I add objects to an <code>ObservableCollection</code> from a background worker thread by utilizing the <code>AddOnUI</code> extension method used in <a href="https://stackoverflow.com/questions/2091988/how-do-i-update-an-observablecollection-via-a-worker-thread">this answer</a>. </p>
  
  <p>The extension method that I am using:</p>
  
  <pre><code>public static void AddOnUI&lt;T&gt;(this ICollection&lt;T&gt; collection, T item)
  {
      Action&lt;T&gt; addMethod = collection.Add;
      Application.Current.Dispatcher.BeginInvoke( addMethod, item );
  }
  
  ...
  
  collection.AddOnUI(new B());
  </code></pre>
  
  <p>This solution works perfectly for collection objects that get added from a background worker thread. However, when manually adding a collection object just on the UI thread with no background thread involved, <strong>the collection does not populate in data, or in the View</strong>. Is there a way to query what thread is active when calling an add method from an <code>ObservableCollection</code>? </p>
  
  <p>Maybe do something like this?:</p>
  
  <pre><code>if(calling from background thread)
      collection.AddOnUI(new B());
  else //Manually adding from UI Thread
      collection.Add(new B());
  </code></pre>
  
  <p><strong>Solution:</strong></p>
  
  <p>My solution ended up being a lot similar to my pseudo code, which I really liked.</p>
  
  <pre><code>if(System.Threading.Thread.CurrentThread.IsBackground)
      collection.AddOnUI(new B());
  else //Manually adding from UI Thread
      collection.Add(new B());
  </code></pre>
  
  <p><strong>Alternate Solution:</strong></p>
  
  <p><code>AddOnUI()</code> (Change <code>BeginInvoke</code> to <code>Invoke</code>):</p>
  
  <pre><code>public static void AddOnUI&lt;T&gt;(this ICollection&lt;T&gt; collection, T item)
  {
      Action&lt;T&gt; addMethod = collection.Add;
      Application.Current.Dispatcher.Invoke(addMethod, item);
  }
  
  collection.AddOnUI(new B());
  </code></pre>