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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:35:25+00:00 2026-05-11T17:35:25+00:00

Suppose I have an IEnumerable such as a List(TValue) and I want to keep

  • 0

Suppose I have an IEnumerable such as a List(TValue) and I want to keep track of whether this list is being accessed (to prevent issues with, say, adding to the list while it is being iterated over on a different thread); I can always write code such as the following:

Dim List1 As New List(Of Integer)
Dim IteratingList1 As Boolean = False

' ... some code ... '

Private Function getSumOfPositivesFromList1() As Integer
    If IteratingList1 Then Return -1

    IteratingList1 = True

    Dim SumOfPositives As Integer = 0

    For Each x As Integer In List1
        If x > 0 Then SumOfPositives += x
    Next

    IteratingList1 = False

    Return SumOfPositives
End Function

(I realize this code is very arbitrary, but it illustrates what I’m talking about.)

My question is whether there’s a better/cleaner way to perform this check than by manually updating and accessing a Boolean, as above. I feel like there must be, but, to my knowledge, there isn’t any IEnumerable class with a built-in “I am being iterated over” method or property. And writing a new class that implements IEnumerable and contains such a property seems like overkill to me.

  • 1 1 Answer
  • 5 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-11T17:35:26+00:00Added an answer on May 11, 2026 at 5:35 pm

    Afaik, In VB.NET you can use the SyncLock to place a lock around an object while accessing it, to prevent other threads from simultaneously working on it.

    Public Shared objStorageLock As New Object
    
    Private Function getSumOfPositivesFromList1() As Integer
    
        Dim SumOfPositives As Integer = 0
    
        SyncLock objStorageLock 
    
        For Each x As Integer In List1
            If x > 0 Then SumOfPositives += x
        Next
    
        End SyncLock
    
        Return SumOfPositives
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an IEnumerable<int> and I want these to be converted into their
Suppose I have a list of strings, like this: var candidates = new List<String>
Suppose I have a List<T> with 1000 items in it. I'm then passing this
Suppose I have an ordered list of float (ascending). I want to remove from
Suppose I have this LINQ query (on .NET 4.0) : IEnumerable<Service> listService = (from
Suppose, I have an IEnumerable<Tuple<string, object>> and that I want to replace for each
Let's suppose I have some method that returns a IEnumerable<int> object. This methods make
Suppose I have the view models below. public class CustomerListViewModel { public IEnumerable<CustomerViewModel> Customers
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a simple model, such as Record: @Model public class Record {

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.