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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:13:42+00:00 2026-05-27T23:13:42+00:00

I am in mid of an MVVM application. Here I have a collection (ObservableCollection)

  • 0

I am in mid of an MVVM application. Here I have a collection (ObservableCollection) that may have add, delete operations done from a different thread.
Here is the code. I have tried to keep it as complete as possible:

private Thread _thread = null;
private Dispatcher _UIDispatcher = null;
public ObservableCollection<string> ListOfStrings { get; private set; }

The ctor:

 public MainWindowViewModel(Dispatcher dispatcher)
 {
            this.ListOfStrings = new ObservableCollection<string>();
            this.StartAsyncCall = new RelayCommand(AsyncCall, CanCallAsynch);
            this._UIDispatcher = dispatcher;
 }

I am taking the View’s dispatcher as a reference (It is not a good practice though as it makes Unit testing difficult. Gary Hall suggested a nice approach using AOP. I can change that later anytime though.). And then this method that I call via a command

private void AsyncCall()
{
    if (this.ListOfStrings.Count > 0)
       this.ListOfStrings.Clear();
     //_backgroundWorker.RunWorkerAsync();
    this._thread = new Thread(new ThreadStart(AddNumbersToList));
    this._thread.IsBackground = true;
    this._thread.Start();
}
 private void AddNumbersToList()
 {
       Action delAddNum = new Action(AddNumbersToList);
       for (int i = 0; i < 100000000; i++)
       {
           if (null != this._UIDispatcher &&
            !this._UIDispatcher.CheckAccess())
                    _UIDispatcher.Invoke(DispatcherPriority.Render,delAddNum);
           else
           {
                    this.ListOfStrings.Add(Convert.ToString(i.ToString()));
           }
        }
  }

The ObservableCollection is bound to a listbox in the View.

The effect that I expected was, as the ObservableCollection gets new numbers added, They should be displayed in the UI. The UI would be nicley responsive too. But strangely the UI is unresponsive. The same thing when I am trying to achieve via a background worker is finely achieved. Can someone please point where am I going wrong. I don’t want to go for the background worker as I want the flexibility of using the more granular Thread class.
Below is the code that I am using with BGWorker and it is running fine:

    private BackgroundWorker _backgroundWorker = new BackgroundWorker();

The CTOR:

public MainWindowViewModel()
{
     this.ListOfStrings = new ObservableCollection<string>();
     this.StartAsyncCall = new RelayCommand(AsyncCall, CanCallAsynch);
     _backgroundWorker.DoWork += AddNumbersToList;
     _backgroundWorker.RunWorkerCompleted += this.LoadResultsCompleted;
     _backgroundWorker.WorkerReportsProgress = true;
     _backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(_backgroundWorker_ProgressChanged);
}

The methods:

 private void AsyncCall()
 {
    if (this.ListOfStrings.Count > 0)
      this.ListOfStrings.Clear();
      this._backgroundWorker.RunWorkerAsync();
 }

private void AddNumbersToList(object sender, DoWorkEventArgs e)
{
      BackgroundWorker bw = sender as BackgroundWorker;
      if(null!=bw)
      for (int i = 0; i < 10; i++)
      {
           bw.ReportProgress(0, i);
                    Thread.Sleep(1000);
        }
 }


 private void LoadResultsCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
   if (e.Error == null)
   {
            this.ListOfStrings.Add("Completed");
            CommandManager.InvalidateRequerySuggested();
   }
 }

//This works just wonderful:
void _backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.ListOfStrings.Add(Convert.ToString(e.UserState.ToString()));
}

Thank you in advance.

..James

  • 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-27T23:13:42+00:00Added an answer on May 27, 2026 at 11:13 pm

    Its the recursion that is hanging your code…

    This works for me…

        private void AsyncCall()
        {
            if (this.MyCollection.Count > 0)
                this.MyCollection.Clear();
    
            this._thread = new Thread(new ThreadStart(AddNumbersToList));
            this._thread.IsBackground = true;
    
            this._thread.Start();
        }
    
        private void AddNumbersToList()
        {
            for (int i = 0; i < 100000000; i++)
            {
                this.Dispatcher.Invoke(
                    new Action(
                        delegate
                            {
                                this.MyCollection.Add(Convert.ToString(i.ToString()));
                            }));
    
                Thread.Sleep(100);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a mid-size SQL Server based application that has no indexes defined. Not
I have a mid-size collection of records --about 20 million -- that I need
We have a mid-sized asp.net project that we just promoted from our dev server
I have a collection of .html files created in the mid-90s, which include a
I have a mid size project that I need to implement on Wordpress. I
I have some mid-large project that is actively using boost libraries and, hence, suffers
I have a mid-size Spring application and I want to insert key/value pairs into
I want to get all midi (*.mid) files from a site that's set up
Recently i completed on mid level web application where i have used telerik controls,
I have a mid section div on my site and an animation that moves

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.