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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:07:58+00:00 2026-05-31T10:07:58+00:00

I wish to iterate over a collection in a thread safe manner. I find

  • 0

I wish to iterate over a collection in a thread safe manner. I find it’s convieniant to have one method called

/// <summary>
/// This visits all items and performs an action on them in a thread manner
/// </summary>
/// <param name="visitAction">The action to perform on the item</param>
public void VisitAllItems(Action<Item> visitAction) {
    lock (listLock) {
        foreach (Item item in this.ItemList) {
            visitAction.Invoke(item);
        }
    }
}

An example of it’s use could be

/// <summary>
/// Saves each item in the list to the database
/// </summary>
protected static void SaveListToDatabase() {
    this.VisitAllItems(item => {
        bool itemSavedSuccessfully = item.SaveToDB();
        if(!itemSavedSuccessfully) {
            //log error message
        }
    });
}

Another example of it’s use would be

/// <summary>
/// Get the number of special items in the list.
/// </summary>
protected int GetNumberOfUnsynchronisedItems() {
    int numberOfSpecialItems = 0;
    this.VisitAllItems((item) => {
        numberOfSpecialItems += item.IsItemSpecial() ? 1 : 0;
    });
    return numberOfSpecialItems;
}

But I’m sure there is a better way of writing the VisitAllItems method to use a Func<> rather than an Action<> delegate to return this value. I’ve tried several things but end up with compilation errors.

Can anyone see a neater way to implement this method?

Thanks,
Alasdair

  • 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-31T10:07:59+00:00Added an answer on May 31, 2026 at 10:07 am

    It all depends on the usage, so it’s hard to tell what exactly would be useful for you. But one way would be to return one result for each item in the collection, and then use LINQ to combine the results.

    public IEnumerable<TResult> VisitAllItems<TResult>(
        Func<Item, Result> visitfunction)
    {
        var result = new List<TResult>();
        lock (listLock)
        {
            foreach (Item item in ItemList)
                result.Add(visitfunction(item));
        }
        return result;
    }
    

    Normally, I would use yield return instead of manually creating the List<T>, but I think it’s better this way here, because of the lock.

    The usage would be like this:

    protected int GetNumberOfUnsynchronisedItems()
    {
        return VisitAllItems(item => item.IsItemSpecial())
                      .Count(isSpecial => isSpecial);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have list of dictonaries. I wish type iterate over this list and map
I have an XML file loaded into a DOM document, I wish to iterate
Hopefully this is a quick one! I have an editable grid using 'clientSide' (local)
In my ASP.NET MVC project, I have a polymorphic collection that I wish to
I have a collection of elements that I need to operate over, calling member
I wish to Avoid duplicated item being inserted. When I iterate through the collection
I have a QuerySet that I wish to pass to a generic view for
I have a string of numbers in C++: like string str=1234567012 I wish to
I have the following map. I wish to map BasketItem to the class Product.
Somebody must have come up with a solution for this by now. We are

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.