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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:59:30+00:00 2026-06-06T22:59:30+00:00

I try to make my ListBox connected to ObservaleCollection be more efficient so for

  • 0

I try to make my ListBox connected to ObservaleCollection be more efficient so for the DB query I implemented a BackgroundWorker to do the job. Then whithin this backgroundworker I want to add every lets say 70 ms 3 entries to the UI, so the UI on larger number of entries (lets say 100) does not get blocked.
Here is the code:

    void updateTMWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        var MessagesInDB = from MessageViewModel tm in MessagesDB.Messages
                                  where tm.Type.Equals(_type)
                                  orderby tm.Distance
                                  select tm;

        // Execute the query and place the results into a collection.
        Dispatcher.BeginInvoke(() => { MessagesClass.Instance.Messages = new ObservableCollection<MessageViewModel>(); });

        Collection<MessageViewModel> tempM = new Collection<MessageViewModel>();
        int tempCounter = 0;

        foreach (MessageViewModel mToAdd in MessagesInDB)
        {
            if (MessagesClass.Instance.Messages.IndexOf(mToAdd) == -1)
            {
                tempM.Add(mToAdd);
                tempCounter = tempCounter + 1;
            }
            if (tempCounter % 3 == 0)
            {
                tempCounter = 0;
                Debug.WriteLine("SIZE OF TEMP:" + tempM.Count());
                Dispatcher.BeginInvoke(() =>
                {
                    // add 3  messages at once
                    MessagesClass.Instance.Messages.Add(tempM[0]);
                    MessagesClass.Instance.Messages.Add(tempM[1]);
                    MessagesClass.Instance.Messages.Add(tempM[2]);
                });
                tempM = new Collection<MessageViewModel>();
                Thread.Sleep(70);
            } 
        }
        // finish off the rest
        Dispatcher.BeginInvoke(() =>
        {
            for (int i = 0; i < tempM.Count(); i++)
            {
                MessagesClass.Instance.Messages.Add(tempM[i]);
            }
        });            
    }

The output is:

SIZE OF TEMP:3

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

in the line: MessagesClass.Instance.Messages.Add(tempM[0]); where the code tries to access the first element of tempM

Any hints whats wrong? Why can’t I access the tempM elements, although the collection size is > 0?

  • 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-06T22:59:32+00:00Added an answer on June 6, 2026 at 10:59 pm

    You forget about thread synchronization. Look at your code:

                1: Debug.WriteLine("SIZE OF TEMP:" + tempM.Count());
                Dispatcher.BeginInvoke(() =>
                {
                    // add 3  messages at once
                    3: MessagesClass.Instance.Messages.Add(tempM[0]);
                    MessagesClass.Instance.Messages.Add(tempM[1]);
                    MessagesClass.Instance.Messages.Add(tempM[2]);
                });
                2: tempM = new Collection<MessageViewModel>();
    

    tempM already will be null when MessagesClass.Instance.Messages.Add(tempM[0]); is executed. So, use some sort or synchronization objects, for example:

                EventWaitHandle Wait = new AutoResetEvent(false);
    
                Debug.WriteLine("SIZE OF TEMP:" + tempM.Count());
                Dispatcher.BeginInvoke(() =>
                {
                    // add 3  messages at once
                    MessagesClass.Instance.Messages.Add(tempM[0]);
                    MessagesClass.Instance.Messages.Add(tempM[1]);
                    MessagesClass.Instance.Messages.Add(tempM[2]);
    
                    Wait.Set();
                });
                // wait while tempM is not in use anymore
                Wait.WaitOne();
    
                tempM = new Collection<MessageViewModel>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll try make this short. I want to get a list of browsers installed
This is really strange behavior. When I try make a search using findItemsAdvanced to
i try to write a project to use Quart.net (http://quartznet.sourceforge.net/) to make job scheduling
Databinding in WPF is great, but the moment you try make things more complex
If I want to make a custom listbox by inheriting from the ListBox class
I want to interrupt a thread1 from another thread2 but when I try make
I try to make an ActiveX by C# with COM-visible. It is a Windows
I try to make an imageview clickable. Actually it is clickable, so that I
I try to make a table header fixed when scoll down on pages. The
I try to make a apps that need barcode scanner, i already can get

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.