List enumeration exception under weird circumstances.

G

Guest

Hi,
I hv foll. code:
foreach(object obj in myChkedlistbox.CheckedItems)
{
Messagebox.show(obj.tostring());
}

It gives me the foll exception:
"The list that this enumerator is bound to has been modified. An enumerator
can only be used if the list does not change."

I dont see where am I changing contents of myChkedlistbox list. What is
more surprising, at the same time the foll. (similar) code just works fine.

for (int i=0; i< dynColumns.CheckedItems.Count;i++)
{
object itemChecked = dynColumns.CheckedItems;
MessageBox.Show(itemChecked.ToString());
}

One more confusing thing is that the exception doesnt come on each run.
Once in a while the exception pops up and then after that its v.consistent
to the point that I reduce my code to:
foreach(object obj in myChkedlistbox.CheckedItems)
{} //Yes empty loop.

& still throws the exception..whoa !!

However If I do a clean-rebuild it goes away and just works :-? until some
mysterios next time when it decides to revisit again.
 
R

Richard Blewett [DevelopMentor]

Do you have another thread running processing the dynColumns.CheckedItems list?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,
I hv foll. code:
foreach(object obj in myChkedlistbox.CheckedItems)
{
Messagebox.show(obj.tostring());
}

It gives me the foll exception:
"The list that this enumerator is bound to has been modified. An enumerator
can only be used if the list does not change."

I dont see where am I changing contents of myChkedlistbox list. What is
more surprising, at the same time the foll. (similar) code just works fine.

for (int i=0; i< dynColumns.CheckedItems.Count;i++)
{
object itemChecked = dynColumns.CheckedItems;
MessageBox.Show(itemChecked.ToString());
}

One more confusing thing is that the exception doesnt come on each run.
Once in a while the exception pops up and then after that its v.consistent
to the point that I reduce my code to:
foreach(object obj in myChkedlistbox.CheckedItems)
{} //Yes empty loop.

& still throws the exception..whoa !!

However If I do a clean-rebuild it goes away and just works :-? until some
mysterios next time when it decides to revisit again.
 
G

Guest

Hi Richard,
Thanks for yr reply.
No, I do not have any other threads running the same. Its a simple one
form,one toolwindow(as a VSIP pkg), serial code.

Rgds,
-sk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top