Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Collection was modified after the enumerator was instantiated

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Collection was modified after the enumerator was instantiated

    Hello

    I very often encounter an error stating that " Collection was modified after the enumerator was instantiated ".
    I have many collections ( Sorted Dictionaries ) in my code and often I loop through the collection ( not removing or adding items just looping through the elements ) while it is being updated in OnRender().

    I had many foreach() loops that I changed for while() loops ( GetEnumerator() ) knowing that foreach() loops can cause problems but I am still facing this problem.

    What do you recommend I should do ?

    Thank you for your help.

    #2
    Hello blar58,

    Thanks for your question.

    I may suggest the following to avoid hitting this issue:

    1. Copy the collection before looping through it. This can often be done by calling ToList() from the collection.
    2. Use lock statements to prevent writes to a collection when they may be read from another part of your code.

    Lock (publicly available resource) - https://docs.microsoft.com/en-us/dot...lock-statement

    An indicator that uses locks between OnRender and OnMarketDepth can be found below which may also give some insight.

    2/8/2019 Fixed issue where bidRows and askRows were not properly locked between MarketDepth and OnRender. Speculator Ed’s Level 2 indicator Press F5 to refresh indicators if level 2 becomes out of sync. changelog: v2.0: – combined different depths with same price (good for stocks) – removed highlighting of 3rd cumulative depth price (complications with combining […]


    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

    ​​​​​​​We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Jim

      Thank you for your quick response.
      I tried copying the collection but seems to freeze indicator for certain instrument.
      I will try the lock statement.
      Will locking the collection introduce delay or slow down in updating the dictionaries ?

      Also what should I lock ?
      The reading or the updating ?

      Thanks
      Last edited by blar58; 05-12-2020, 02:19 PM.

      Comment


        #4
        Hello blar58,

        I would not expect a performance impact using lock statements.

        Locks should be used on the collection whenever accessed or written to.

        As this is a general C# issue, you could find some resources external to NinjaTrader on this type of issue to research other solutions. A publicly available stack overflow article can be found below.

        Recently I came across the error "Collection was modified after the enumerator was instantiated" and I have no idea why. The error specifies the web.config. What causes this error? I haven't bee...


        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thank you Jim.
          I am trying to download the indicator above but the link doesn't work.

          Comment


            #6
            Hello blar58,

            Thanks for your reply.

            I am having no difficulty downloading from the link you provided. We'll need more information to find out why you're unable to download from our Ecosystem site. Please answer all of the following questions:
            • What version of Windows are you using?
            • What web browser are you using?
            • Does this symptom persist if your clear your browser's cache?
            • What about if you use a different web browser?
            • What about after you turn off your PC, turn off your modem/router for at least 30 seconds, then restart your PC?
            Please let me know answers to these questions and if we are still stuck, I'll provide a direct link.

            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              The reason why your staff all seem to be able to download and so many of us cannot is because you are on the same domain. The issue would appear to be cookie-related, and seems to be related to a problem with implementing the "SameSite" attribute. I have attached a picture.

              Click image for larger version  Name:	NTEcoSystemDownloadIssue.png Views:	0 Size:	282.6 KB ID:	1099804

              Windows 10 ver Version 10.0.18362.778
              Multiple browsers: Firefox, Chrome, Vivaldi, Opera, Slimjet all show the problem.
              The problem shows up on 3 Virtual Machines and 4 physical machines.
              The modem is automatically restarted every Sunday at 0400 ET.
              This happens on one machine where I deleted all stored data.
              On the same computer, I uninstalled Chrome, axed the Chrome user folders, rebooted, and reinstalled Chrome. The same problem manifested.
              Last edited by koganam; 05-15-2020, 08:23 AM.

              Comment


                #8
                Thank you Koganam

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by lightsun47, Today, 03:51 PM
                0 responses
                5 views
                0 likes
                Last Post lightsun47  
                Started by 00nevest, Today, 02:27 PM
                1 response
                10 views
                0 likes
                Last Post 00nevest  
                Started by futtrader, 04-21-2024, 01:50 AM
                4 responses
                46 views
                0 likes
                Last Post futtrader  
                Started by Option Whisperer, Today, 09:55 AM
                1 response
                14 views
                0 likes
                Last Post bltdavid  
                Started by port119, Today, 02:43 PM
                0 responses
                10 views
                0 likes
                Last Post port119
                by port119
                 
                Working...
                X