title: WPF Multithreading: Using Dispatcher but the UI still hangs?
body: <p>I am having a bit of a problem with my UI hanging even though I am using a Dispatcher, and before I get any further I'm wondering if it's the way I'm handling the data retrieval.</p>
  
  <p>Right now I have my main window creating a View and ViewModel. Then, inside of a new Thread (using a Dispatcher) it sets the View.DataContext = ViewModel. A very large ObservableCollection is lazily created when the binding kicks in which is causing the slowdown. However, it seems that some of the other UI items that should be showing up before that slowdow don't actually show up.</p>
  
  <pre><code>   private void ButtonClick(Object sender, RoutedEventArgs e)
     {
          MyView view = new MyView();
          MyViewModel vm = new MyViewModel();
  
          TabItem tabItem = new TabItem();
          tabItem.Header = "MyView";
          tabItem.Content = view;
  
          MyTabCollection.Items.Add(tabItem);
  
          Window working = new Working();
          working.Show();
  
          ThreadStart thread = delegate()
          {
              DispatcherOperation operation = Dispatcher.BeginInvoke(
                  DispatcherPriority.Normal,
                  new Action(delegate()
                  {
                      view.DataContext = vm;
                      ((FrameworkElement)view.Parent).Focus();
                      working.Close();
                  }
                  )
              );
          };
  
          Thread theThread = new Thread(thread);
          theThread.Start();
      }
  </code></pre>
  
  <p>This basically says it's supposed to create a view and a viewmodel, then add the view to the tab collection I have (which means it should show the new tab at the least). And, it should also show a "Working..." window. After that, a separate thread is supposed to link the ViewModel to the view, focus on that tab and close the working window. The problem is that the first portion doesn't show until everything is done; The tab is not displayed and the working window is not shown until after the new Thread actually finishes (which causes the Working window to show/close right away). I'm guessing it might have to do with the way I retrieve the data, but I'm not sure. Here is the way it does it:</p>
  
  <ol>
  <li>Create View</li>
  <li>Create ViewModel</li>
  <li>Create TabItem with Content set to the View and add the TabItem to the TabCollection.</li>
  <li>Create/Show the "Working..." window</li>
  <li>Dispatcher: Set the View.DataContext = ViewModel. This event sets off the DataBindings, which in turn grab the ObservableCollection. Since the OC is created Lazily it is now being created (this is the bottleneck). &lt;-- Is this messing up my separate thread/dispatcher?</li>
  <li>Dispatcher: Set Focus to the tab</li>
  <li>Close the "Working..." window</li>
  </ol>