none
Regarding the code error System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.' RRS feed

  • Question

  • Hi,

    While updating my chart with the collected values. I'm getting 

    System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.' i don't know why it is coming. Can anyone help me to over come this problem or Is there any exception code to avoid this error

    regards

    B Teja


    B TEJA

    Wednesday, May 23, 2018 7:18 AM

All replies

  • You are running in a loop using a collection or list and the loop index counter is being modified outside the loop.

    Similar to this:

    For I = 1 to mylist.count -1

      'here you modify mylist.count and that changes the index I total

        mylist.add(something)

    next

    Wednesday, May 23, 2018 7:22 AM
  • While updating my chart with the collected values. I'm getting 

    System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.' i don't know why it is coming. Can anyone help me to over come this problem or Is there any exception code to avoid this error

    You can't make changes to a collection, such as adding or removing items from the collection, while there is a loop that is iterating over the items in the collection.

    Wednesday, May 23, 2018 7:48 AM
  • Hi

    Yes exactly you got the point. I am trying this code in chart update

    While (_run)
                If DRDY = True Then

                    DRDY = False

                    Chart1.Series("Voltage").Points.Clear()
                    CheckForIllegalCrossThreadCalls = False

                    For i = 0 To 3000

                        If i <= 3000 Then

                            If count = 0 Then


                                Chart1.Series("Voltage").Points.AddXY(Val(i), Val(Buffer1(count, i)))

                                ' Label_Voltage_CH3.Text = "Voltage:   " + vbTab + Buffer1(count, i).ToString("f4") + "V"

                                If stopthread = False Then

                                    Exit Sub

                                End If

                            End If
                        End If

                    Next
                    i = 0
                    'System.Threading.Thread.Sleep(1000)
                End If
                ' System.Threading.Thread.Sleep(1003)

            End While

    in this count and DRDY is updating outside the loop. I don't know how it is giving the error.


    B TEJA

    Wednesday, May 23, 2018 8:19 AM
  • Hi

    Yes exactly you got the point. I am trying this code in chart update


    in this count and DRDY is updating outside the loop. I don't know how it is giving the error.


    B TEJA

    I cant see it in the code you show.

    What do you mean "trying this code in chart update" ? Do you mean paint event? What is chart update?

    Well you cant use the chart series points.count in your loop counts anywhere or you will get the error. There are several places where it appears we don't see all the code. I think you just need to keep hunting. Don't use the chartseries point count etc.

    Maybe Acamar can spot it?

    Wednesday, May 23, 2018 8:36 AM
  • Hi,

    Can you suggest me the right way to display the chart. i have a bulk of data 4000samples which was running ina multi thread.

    regards

    b teja


    B TEJA

    Wednesday, May 23, 2018 9:33 AM
  • Hi,

    Can you suggest me the right way to display the chart. i have a bulk of data 4000samples which was running ina multi thread.

    regards

    b teja


    B TEJA

    As Tommy said, there is no sign of the cause of this error in the code you posted.  However... the fact that you have this statement:

    CheckForIllegalCrossThreadCalls = False

    Tells us that your multithreading code is almost certainly incorrect and so it may be that poor threading is causing the error in an unpredictable way.  The code you posted may be tripping over some other code that you have not shown.

    It isn't clear why you are using multithreading or what you are trying to achieve with it, but you should review these topics/concepts:

    https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/index

    https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming

    https://docs.microsoft.com/en-us/dotnet/standard/threading/index


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 23, 2018 11:44 AM
    Moderator
  • Hi,

    Can you suggest me the right way to display the chart. i have a bulk of data 4000samples which was running ina multi thread.

    regards

    b teja


    B TEJA

    I think so but you have to tell us exactly what you are doing and what you want to draw.

    Did the last example I posted in this thread help you? Or is this a new chart?

    Are you reading a sensor or what ? What is the data? Why are you multithreading in the code you show above?

    Describe the purpose of the application please. Maybe post a picture of the final result.

    Wednesday, May 23, 2018 1:06 PM