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 747017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:09:26+00:00 2026-05-14T14:09:26+00:00

I seem to be going round in circles. I have a WPF application that

  • 0

I seem to be going round in circles.

I have a WPF application that has a main ribbon window with a status bar. When you navigate to a “view” a user control is displayed as the content of the main window.

The view has a ViewModel which handles retrieving data from the database and the View’s datacontext is set to the ViewModel.

What I want is to have the lengthy operation (data retrieval) run on a background thread and whilst it is running the status in the main window to report appropriately. When the background task is complete, the status should revert back to “Ready” (much the same as Visual Studio).

How should I wire this together so that I can have the data access code separated out in the ViewModel whilst keeping a responsive UI?

I have tried using the BackgroundWorker is various places in the code and I still end up with an unresponsive UI.

  • 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-14T14:09:26+00:00Added an answer on May 14, 2026 at 2:09 pm

    I find BackgroundWorker’s interface inconvenient for this purpose so I prefer to stick to using ThreadPool directly.

    Here’s the basic idea:

    public class MyViewModel
    {
      public SomeCollectionType<Widget> Widgets
      {
        get
        {
          if(!WidgetFillQueued)
          {
            WidgetFillQueued = true;
            ThreadPool.QueueUserWorkItem(_ =>
            {
              WidgetLoadStatus = 0;
              int totalCount = FetchWidgetCount();
              while(_internalWidgetCollection.Count < totalCount && !AbortFill)
              {
                _internalWidgetCollection.AddRange(FetchMoreWidgets());
                WidgetLoadStatus = (double)_internalWidgetCollection.Count / totalCount;
              }
              WidgetLoadStatus = null;  // To indicate complete
            });
          }
          return _internalWidgetCollection;
        }
      }
    
    }
    

    Assuming WidgetLoadStatus is a DependencyProperty that is bound from the UI, WPF’s binding system takes care of the thread transitions required to keep the status bar progress display updated.

    Assuming _internalWidgetCollection allows multi-threaded access and implements INotifyPropertyChanged properly, all collection updates will also result in UI updates on the UI thread.

    In my actual view models there are many collections so instead of using individual properties like WidgetFillQueued and WidgetLoadStatus, I use a data structure that keeps track of all currently executing operations and computes a combined status value for display. But the above code gives the basic idea of how the threading can be implemented properly.

    The above is also applicable to loading a single large object such as a file download: Instead of calling AddRange() every time, just accumulate the data until it is all downloaded, then set the property containing the data. Note that if the object itself includes DispatcherObjects it must be deserialized on the UI thread. Do this by calling Dispatcher.BeginInvoke from within the thread.

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

Sidebar

Related Questions

No related questions found

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.