Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 951799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:47:37+00:00 2026-05-15T23:47:37+00:00

I’m using MVVM Light to build a WP7 (Windows Phone 7) application. I wish

  • 0

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.

I have already found out that I cannot invoke a Delegate asynchronously from an WP7 app.

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).

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?

Thanks in advance,
Jeff.

Edit – Here is a sample Model

public class DataModel
{
    public event EventHandler<DataLoadingEventArgs> DataLoadingComplete;
    public event EventHandler<DataLoadingErrorEventArgs> DataLoadingError;
    List<Data> _dataCasch = new List<Data>();

    public void GetData()
    {
        ThreadPool.QueueUserWorkItem(func =>
        {
            try
            {
                LoadData();
                if (DataLoadingComplete != null)
                {
                    //Dispatch complete event back to the UI thread
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                       //raise event 
                        DataLoadingComplete(this, new DataLoadingEventArgs(_dataCasch));
                    });
                }
            }
            catch (Exception ex)
            {
                if (DataLoadingError != null)
                {
                    //Dispatch error event back to the UI thread
                    DispatcherHelper.CheckBeginInvokeOnUI(() => 
                    {
                        //raise error
                        DataLoadingError(this, new DataLoadingErrorEventArgs(ex));
                    });
                }
            }
        });
    }

    private void LoadData()
    {
        //Do work to load data....
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:47:38+00:00Added an answer on May 15, 2026 at 11:47 pm

    Here’s how I’d approach a solution to this.

    Your ViewModel implements INotifyPropertyChanged right? There’s no need to dispatch the Events. Just raise them “bare” in the Model, then dispatch the RaisePropertyChanged in the ViewModel.

    And yes, you should have some sort of singleton model/database in your code. After all, what is a SQL Database if not some gigantic singleton? Since we don’t have a database in WP7, don’t be shy creating a singleton object. I have one called “Database” 🙂

    I’ve just tried threading my dataloads in there, and realise that in fact the best approach is simply implementing INotifyPropertyChanged right down at the model level. There’s no shame in this.

    So given that, here’s what I’m doing in the singleton Database object to load and return my Tours “table” (note the thread.sleep to make it take a visible amount of time to load, normally its sub 100ms). Database class now implements INotifyPropertyChanged, and raises events when loading is completed:

    public ObservableCollection<Tour> Tours
    {
      get
      {
        if ( _tours == null )
        {
          _tours = new ObservableCollection<Tour>();
          ThreadPool.QueueUserWorkItem(LoadTours);
        }
        return _tours;
      }
    }
    
    private void LoadTours(object o)
    {
      var start = DateTime.Now;
      //simlate lots of work 
      Thread.Sleep(5000);
      _tours = IsoStore.Deserialize<ObservableCollection<Tour>>( ToursFilename ) ??  new ObservableCollection<Tour>();
      Debug.WriteLine( "Deserialize time: " + DateTime.Now.Subtract( start ).ToString() );
      RaisePropertyChanged("Tours");
    }
    

    You follow? I’m deserializing the Tour list on a background thread, then raising a propertychanged event.

    Now in the ViewModel, I want a list of TourViewModels to bind to, which I select with a linq query once I see that the Tours table has changed. It’s probably a bit cheap to listen for the Database event in the ViewModel – it might be “nicer” to encapsulate that in the model, but let’s not make work we we don’t need to eh?

    Hook the Database event in the Viewmodel’s constructor:

    public TourViewModel()
    {
    Database.Instance.PropertyChanged += DatabasePropertyChanged;
    }
    

    Listen for the appropriate table change (we love magic strings! 😉 ):

    private void DatabasePropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      if(e.PropertyName == "Tours")
      {
        LoadTourList();
      }
    }
    

    Select the records I want from the table, then tell the view there is new data:

    public void LoadTourList()
    {
      AllTours = ( from t in Database.Instance.Tours
        select new TourViewModel( t ) ).ToList();
    
      RaisePropertyChanged( "AllTours" );
    }
    

    And lastly, in your ViewModelBase, it’s best to check if your RaisePropertyChanged needs dispatching. My “SafeDispatch” method is pretty much the same as the one from MVVMlight:

    private void RaisePropertyChanged(string property)
    {
      if ( PropertyChanged != null )
      {
        UiHelper.SafeDispatch(() =>
          PropertyChanged(this, new PropertyChangedEventArgs(property)));
      }
    }
    

    This works perfectly in my code, and I think is fairly tidy?

    Lastly, extra for experts: in WP7, it might be good to add a ProgressBar with IsIndeterminate=True to your page – this will display the “dotted” progress bar. Then what you can do is when the ViewModel first loads you could set a “ProgressBarVisible” property to Visible (and raise the associated PropertyChanged event). Bind the ProgressBar’s visibility to this ViewModel property. When the Database PropertyChanged event fires, set the visibility to Collapsed to make the progressbar go away.

    This way the user will see the “IsIndeterminate” progress bar at the top of their screen while the deserialization is running. Nice!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.