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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:27:35+00:00 2026-06-04T03:27:35+00:00

I have an example here that replicates what I am trying to accomplish. As

  • 0

I have an example here that replicates what I am trying to accomplish.
As the following code will show – I have ViewModel that updates a ObservableCollection Property bound to a View. Normally I would update the collection from a result retrieved from a Model, but hopefully this example will suffice.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Controls;

namespace MVVMWpf.ViewModel
{
    public class ListViewModel
    {

        public ObservableCollection<int> SomeObjectCollection { get; set; }

        public ListViewModel()
        {

            SomeObjectCollection = new ObservableCollection<int>();

        }

        public void Do()
        {
             for (int i = 1; i < 1000000; i++)
             {
                 int i1 = i;
                 SomeObjectCollection.Add(i1);
             }
        }

    }
}

Unfortunately, this blocks this UI. It will only update the View when loop runs to completion. The way I solved it breaks MVVM concepts. That’s why I need your help. I did it this way.

public class ListViewModel
{
    private delegate void LongRunningProcess();
    public ObservableCollection<int> SomeObjectCollection { get; set; }
    private ListBox listBox;
    public ListViewModel(ListBox listBox)
    {
        this.listBox = listBox;
        SomeObjectCollection = new ObservableCollection<int>();

    }

    public void Do()
    {
        Thread thread = new Thread(() =>
        {
           for (int i = 1; i < int.MaxValue; i++)
           {
               int i1 = i;
               listBox.Dispatcher.Invoke(
                   new LongRunningProcess(() =>
                   SomeObjectCollection.Add(i1);
                 }});

        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }

}

As you can see the ViewModel now knows about the listBox element from the UI. And looking at the MVVM diagram only the View should have a reference to the ViewModel through binding. How does one overcome this issue?
Thanks.

  • 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-06-04T03:27:37+00:00Added an answer on June 4, 2026 at 3:27 am

    You need to let your loop unleash updates to the screen – some kind of DoEvents() will do:

    public static void DoEvents() 
    { 
        Application.Current.Dispatcher.Invoke(
        DispatcherPriority.Background,new Action(delegate { })); 
    }
    

    Add it and call that from within your loop.


    Using timer as another option, your code should look somthing like that:

    private System.Timers.Timer operationsTimer = new System.Timers.Timer();
    private int x;
    

    in your ctor:

    operationsTimer.Elapsed += new System.Timers.ElapsedEventHandler 
    (operationsTimer_Elapsed);
    operationsTimer.Enabled = true;
    

    in your timer elpased:

    operationsTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    { 
        operationsTimer.Enabled = false;
        //add item to collection code
        x++;
        if(x<100)
            operationsTimer.Enabled = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
I currently have a csv file that I'm parsing with an example from here:
As a toy example, suppose that we have a function called 'my_func' (the code
I have an array as the following: function example() { /* some stuff here
We have the following example table (actually taken from another example here on stackoverflow...)
I have an example here: int runcmd(char *cmd) { char* argv[MAX_ARGS]; pid_t child_pid; int
I have seen the example here . All well and good and I understand
I have a simple java project (adapted from the example here ), which is
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
Here's the problem. I ,for example,have a string 2500.Its converted from byte array into

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.