Collection modifying problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nondos

    #1

    Collection modifying problem

    Hello



    I have a thread that run loop in queue variable like this:



    For each <somethingas <somethingin <queue variable>

    Next



    And the thread and the loop work fine



    I have problem when i want to add something to the queue and the thread is
    running i get error message "Collection was modified; enumeration operation
    may not execute." Is there any way to solve this? I thought about this:

    For I=1 to queue.count

    Next

    And when the loop end check if there's new items on queue



    But I prefer if there is another solution with first loop



    Thanks



    P.S sorry for my English

  • Armin Zingler

    #2
    Re: Collection modifying problem

    "nondos" <nondos@hotmail .comschrieb
    Hello
    >
    >
    >
    I have a thread that run loop in queue variable like this:
    >
    >
    >
    For each <somethingas <somethingin <queue variable>
    >
    Next
    >
    >
    >
    And the thread and the loop work fine
    >
    >
    >
    I have problem when i want to add something to the queue and the
    thread is running i get error message "Collection was modified;
    enumeration operation may not execute." Is there any way to solve
    this? I thought about this:
    >
    For I=1 to queue.count
    >
    Next
    >
    And when the loop end check if there's new items on queue
    >
    >
    >
    But I prefer if there is another solution with first loop
    No. Not when using For-Each.

    Even using the second loop isn't the best solution, because the loop's end
    value is evaluated when starting the loop, not each time. So, If queue.count
    is modified during the loop, the initial value of queue.count is the number
    of iterations performed.

    A Do-Loop would fit better:

    Do until i>queue.count
    if bla then
    ...
    i +=1
    else
    remove item from queue
    'no i += 1 here
    end if
    Loop



    Armin

    Comment

    • nondos

      #3
      Re: Collection modifying problem

      thanks

      Comment

      • =?windows-1252?Q?G=F6ran_Andersson?=

        #4
        Re: Collection modifying problem

        Armin Zingler wrote:
        Even using the second loop isn't the best solution, because the loop's end
        value is evaluated when starting the loop, not each time. So, If
        queue.count
        is modified during the loop, the initial value of queue.count is the number
        of iterations performed.
        >
        A Do-Loop would fit better:
        >
        Do until i>queue.count
        if bla then
        ...
        i +=1
        else
        remove item from queue
        'no i += 1 here
        end if
        Loop
        >
        >
        >
        Armin
        >
        That's not a good solution either. Adding items to a queue is not a
        thread safe operation, so what the OP really needs is some proper thread
        safety, not another way of looping the items.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • Armin Zingler

          #5
          Re: Collection modifying problem

          "Göran Andersson" <guffa@guffa.co mschrieb
          Armin Zingler wrote:
          Even using the second loop isn't the best solution, because the
          loop's end value is evaluated when starting the loop, not each
          time. So, If queue.count
          is modified during the loop, the initial value of queue.count is
          the number of iterations performed.

          A Do-Loop would fit better:

          Do until i>queue.count
          if bla then
          ...
          i +=1
          else
          remove item from queue
          'no i += 1 here
          end if
          Loop



          Armin
          >
          That's not a good solution either. Adding items to a queue is not a
          thread safe operation, so what the OP really needs is some proper
          thread safety, not another way of looping the items.

          What else he does is not my problem. His problem was that modifying an
          object that is used with For-Each, does not work. I have shown a general
          solution to iterate through a list while it is being modified. The error he
          gets is not a threading problem in first place.


          Armin

          Comment

          Working...