title: How to run a function on a background thread for Windows Phone 7?
body: <p>I'm using MVVM Light to build a WP7 (Windows Phone 7) application.  I wish to have all the work performed by the Model to be run on a background thread.  Then, when the work is done, raise an event so that the ViewModel can process the data.</p>
  
  <p>I have already found out that I cannot invoke a Delegate asynchronously from an WP7 app.  </p>
  
  <p>Currently I am trying to use ThreadPool.QueueUserWorkItem() to run some code on a background thread and use MVVM Light's DispatcherHelper.CheckBeginInvodeOnUI() to raise an event on the UI thread to signal the ViewModel that the data has been loaded (this crashes VS2010 and Blend 4 when they try to display a design-time view).</p>
  
  <p>Is there any sample code to run some code on a background thread and then dispatch an event back to the UI thread for a WP7 app?</p>
  
  <p>Thanks in advance,
  Jeff.</p>
  
  <p>Edit - Here is a sample Model</p>
  
  <pre><code>public class DataModel
  {
      public event EventHandler&lt;DataLoadingEventArgs&gt; DataLoadingComplete;
      public event EventHandler&lt;DataLoadingErrorEventArgs&gt; DataLoadingError;
      List&lt;Data&gt; _dataCasch = new List&lt;Data&gt;();
  
      public void GetData()
      {
          ThreadPool.QueueUserWorkItem(func =&gt;
          {
              try
              {
                  LoadData();
                  if (DataLoadingComplete != null)
                  {
                      //Dispatch complete event back to the UI thread
                      DispatcherHelper.CheckBeginInvokeOnUI(() =&gt;
                      {
                         //raise event 
                          DataLoadingComplete(this, new DataLoadingEventArgs(_dataCasch));
                      });
                  }
              }
              catch (Exception ex)
              {
                  if (DataLoadingError != null)
                  {
                      //Dispatch error event back to the UI thread
                      DispatcherHelper.CheckBeginInvokeOnUI(() =&gt; 
                      {
                          //raise error
                          DataLoadingError(this, new DataLoadingErrorEventArgs(ex));
                      });
                  }
              }
          });
      }
  
      private void LoadData()
      {
          //Do work to load data....
      }
  }
  </code></pre>