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

  • Home
  • SEARCH
  • 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 6063349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:09:21+00:00 2026-05-23T09:09:21+00:00

One of my WPF element is data-bound to a property that calls Count(Func) on

  • 0

One of my WPF element is data-bound to a property that calls Count(Func) on a IEnumerable. The property displays something like the count of active entities in the system (thus the need of a Func parameter).

public int ActiveEntitiesCount
{
    get
    {
        return Entities.Count((item) =>
        {
            //my own code, just a couple of value comparisons, very quick
        });
    }
}

.
However the IEnumerable is a list that can be changed by another thread at any time. Sometimes, the application crashes, apparently because the IEnumerable is modified while the Count function enumeration is in progress.

I can put a try-catch block, but how to get the value to be returned by that property?

Note: Using lock may be not practical, because I don’t know all the codes that’s accessing the IEnumerable

  • 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-23T09:09:21+00:00Added an answer on May 23, 2026 at 9:09 am

    You can alleviate the issue by making a snapshot of your list before running Count on it. Like:

    return Entities.ToList().Count(item => ...
    

    To truly fix the issue, you would either make it thread-safe, using any of synchronization technique or to redesign it to avoid contention.

    [EDIT] Possible solution:

    public class MyCollection<T> : Collection<T>
    {
        private readonly object syncRoot = new object();
    
        protected override void SetItem(int index, T item)
        {
            lock (syncRoot)
                base.SetItem(index, item);
        }
    
        protected override void InsertItem(int index, T item)
        {
            lock (syncRoot)
                base.InsertItem(index, item);
        }
    
        protected override void ClearItems()
        {
            lock (syncRoot)
                base.ClearItems();
        }
    
        protected override void RemoveItem(int index)
        {
            lock (syncRoot)
                base.RemoveItem(index);
        }
    
        public new int Count(Func<T, bool> predicate)
        {
            lock (syncRoot)
                return Enumerable.Count(this, predicate);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to use such functionality of windows-7 with WPF (one that uses Itunes -
How to create WPF Application Preloader (like one on this Word 2010 Prt Scrn)?
I would like to create a custom WPF control that inherits from comboBox. So
In WPF app inside a TabControl there is a TabItem element with one Label
One of the things I really like with WPF is the extent to which
I'm having one thread that generates GUI elements in wpf. A canvas is there
I have a WPF application that has just one button. When the button is
I'm dynamically building a WPF FlowDocument from a datasource. One of the data elements
I'm writing an InformationKiosk WPF application that displays different full screen slides (or views),
I have a WPF application that uses MVVM data bindings. I am adding items

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.