473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

collection modified, lock ?

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 disconnected, I remove the handle for the object,
no problem.

But in a function, I wan't to loop into each connected client and call a
function on each client.
For that, I do a foreach on my collection of clients, and I call the
function.
BUT I call asynchronously the function, so, when the call is terminated, it
check for the existense of the client, if problem, the client is removed
from the list
=The list is modified AND a I would do a foreach on the collection =>
Sometime, BUG.

I tried to do a lock on the collection (see the code above), but that don't
work. the client is (sometime) removed before the foreach is at the end.

How can I block the remove when I'm in the foreach ?

Thx
public void
AttachMessageHandler(Dufour.InterfaceBascule.Messa geHandlerBascule MsgHnd)

{

lock (RemoteClients)

{

foreach (DictionaryEntry Client in RemoteClients)

{

AsyncDelegateCurrentPesee d = new
AsyncDelegateCurrentPesee(((Dufour.InterfaceBascul e.MessageHandlerBascule)Client.Value).OnServerEven tCurrentPoids);

PoolCallback p = new PoolCallback((String)Client.Key, d);

d.BeginInvoke(LastPesee, CallbackTerminatedCurrentPesee, p);

}

}

public void CallbackTerminatedCurrentPesee(IAsyncResult ar)

{

try

{

AsyncDelegateCurrentPesee d =
((AsyncDelegateCurrentPesee)((PoolCallback)ar.Asyn cState).Delegate);

d.EndInvoke(ar);

}

catch (Exception)

{

lock (RemoteClients)

{

RemoteClients.Remove(((PoolCallback)ar.AsyncState) .uri);

}

}

}
May 23 '07 #1
0 1484

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

Similar topics

3
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
3
by: Anders Both | last post by:
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...
5
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) {...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
28
by: Michael Primeaux | last post by:
What is the recommended pattern for implementing a synchronized (thread-safe) class that inherits from Collection<T>? For example, I want to implement a SyncRoot property . I do see where I can...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
4
by: DC | last post by:
Hi, I am porting an app from Framework 1.1 to 2.0 and I am now using Dictionary objects instead of Hashtables. There are some lock (theHashtable.SyncRoot) statements in the 1.1 code and I...
5
by: sstory | last post by:
I have a class in vb.net that Inherits Generic.List(Of myclass) I want to have totals in this class that are static...i.e. not recalculated every time. They should be calculated once and never...
1
by: Michel Racicot | last post by:
When executing the following function, I get this error message: Collection was modified; enumeration operation may not execute. The only thing that can modify lstClients is when I add one in...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.