473,890 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Collection was modified; enumeration operation may not execute . Exception ?

My client-server system, is begining (when more client are connected)
sometimes to throw an exception like this:

Collection was modified; enumeration operation may not execute.

foreach (tcClient per in tcClientList) <-- Exception throw by this line.
{
if (tcClient!=tcCl ientNow) tcClient.flush( );
}

Does someone have a good advice about how to handle this situation. I know
that I can lock and use volatile and stuff. But the performance of the
system is wery important, and I feel that videly use of locking and
volatile, can cause slover performance.

1. How can I lock the enumeration so that other thread cannot alter or add
element to tcClientList, while enumerating.

2. How can I just ignore the exception completly.

Best Regards

Anders Both, Denmark.
Nov 15 '05 #1
3 54610
the code should be :
foreach (TcClient tcClient in tcClientList) <-- Exception throw by this
line.
{
if (tcClient!=tcCl ientNow) tcClient.flush( );
}
to make sence, sorry. (but ofcause the question is still very valid)
"Anders Both" <an********@hot mail.com> wrote in message
news:#K******** ******@TK2MSFTN GP11.phx.gbl...
My client-server system, is begining (when more client are connected)
sometimes to throw an exception like this:

Collection was modified; enumeration operation may not execute.

foreach (tcClient per in tcClientList) <-- Exception throw by this line.
{
if (tcClient!=tcCl ientNow) tcClient.flush( );
}

Does someone have a good advice about how to handle this situation. I know
that I can lock and use volatile and stuff. But the performance of the
system is wery important, and I feel that videly use of locking and
volatile, can cause slover performance.

1. How can I lock the enumeration so that other thread cannot alter or add
element to tcClientList, while enumerating.

2. How can I just ignore the exception completly.

Best Regards

Anders Both, Denmark.

Nov 15 '05 #2
The error means that you run the risk of stepping over items in the list
if some items are removed or added.
It's safer to use a for loop in this case, also if flush actually removes
an item from the list it's better to traverse the list in reverse order

for(int i = tcClientList.Co unt - 1; i >= 0; i--)
{
TcClient tcClient = (TcClient)tcCli entList[i];

if(tcClient != tcClientNow)
{
tcClient.flush( );
}
}

Still not sure why you would get this error as I can't see anything being
added or removed

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabled
Nov 15 '05 #3
The for loop is not any safer, you can keep the foreach statement!

Your problem is probably due to another thread modifying the list while you
enumerate it.
So, you need synchronization like:

lock(tcClientLi st.SyncRoot)
{
foreach (TcClient tcClient in tcClientList)
...
}

You also need the lock(tcClientLi st.SyncRoot) in the methods that modify the
list. This way, the list cannot be modified by another thread while you are
enumerating it.

Bruno.

"Morten Wennevik" <Mo************ @hotmail.com> a écrit dans le message de
news:opr12bc1iy hntkfz@localhos t...
The error means that you run the risk of stepping over items in the list
if some items are removed or added.
It's safer to use a for loop in this case, also if flush actually removes
an item from the list it's better to traverse the list in reverse order

for(int i = tcClientList.Co unt - 1; i >= 0; i--)
{
TcClient tcClient = (TcClient)tcCli entList[i];

if(tcClient != tcClientNow)
{
tcClient.flush( );
}
}

Still not sure why you would get this error as I can't see anything being
added or removed

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabled
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2283
by: Chuck Bowling | last post by:
I have a collection that I want to edit. When I remove an object from the collection and then try to enumerate thru it with the foreach statement it throws an exception - "Collection was modified; enumeration operation may not execute." Can somebody tell me how to fix this?
5
2661
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) { Console.WriteLine(s.StudentID); }
2
2060
by: Mike | last post by:
I am trying to utilize Hashtable to facilitate mapping one value to another. I have successfully added several such pairs. When using the enumerators to find keys upon subsequent manipulation, I get the following error. Any ideas why this is? Maybe I didn't initialize the table properly? An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll Additional information: Collection was modified; enumeration...
1
1710
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to the 'locations selected' (cart) listbox. I have a hirarchy of locations - main locations and sub locations. main locations being the parent locations, and the sub locations being the child locations. I want to be able to check to see if the...
1
8868
by: John Braham | last post by:
I hope someone can hep with this! I have a winform (VB.NET 2005), containing a tab control, which in turn contains a few standard controls and one custom control, the form used to work fine, then today for no apparent reason when I try to load the designer for the form I am given a message box saying that the tab control has thrown an unhandled exception and has been disabled: Operation unavailable (Exception from HRESULT: 0x800401E3...
4
1706
by: Daniel | last post by:
Hi guys In regards to the foreach and for loops. I am trying to avoid this exception:exception: "Collection was modified; enumeration operation may not execute. System.InvalidOperationException" The problem is i have a timer, that checks at an interval for any changes to a list if there are changes it removes the corresponding item:
0
1512
by: Geoffrey | last post by:
Hello, I work with .net remoting,to simplify, when the client connected, he send an handle to an object, the object is used to exchange message and events. no problem. In my server, I keep a list of all actives clients and the handle of each., no problem In remoting, it's difficult to see when a client is disconnected, so I use asynchronous call to contact the client and if I got an error when I call EndInvoke =the client is...
1
5674
by: SaradaV | last post by:
Hi all, I am working on vb.net web application and Often getting the following error. But even with the same inputs it is not reproducable. In most of the cases its executing successfully and in some rare (unknown) cases only its failing. The interesting point is; I just noted one set of the inputs caused the problem and tried to reproduce the same problem but not able to reproduce the error. One time error came at the firstline of the...
1
2236
by: Abdul Khadhar | last post by:
For Each row As DataRow In edt.Rows dr = dt.NewRow dr("slno") = i dr("item_cd") = Convert.ToString(row("item_cd")) dt1 = sp.FillDataTable("select ITEM_CD,SIBPART_CD from m033sibpart where comp_cd='" & Session("CompCd") & "' and ITEM_CD='" & row("item_cd") & "'") dr("SIBPART_CD") = dt1.Rows(0)("SIBPART_CD") dr("qty") =...
0
9826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.